0

I am trying to pass an argument to my @RESTController Spring Boot class.

In the @POSTMapping method I want to use a method of a self defined Java class for processing the received body and returning a response.

The Spring application is launched in Application.java. The Controller-Object seems to get created implicitly.

I already tried adding a constructor to my RESTController class. But I couldn't find a way to call that constructor with an argument.

// Application.java
public static void main (String[] args) {
    SpringApplication.run(Application.class, args);
}
//ConnectorService.java
@RestController
public class ConnectorService {

    private Solveable solver;

    public ConnectorService() {}

    public ConnectorService (Solveable solveable) {
        this.solver = solveable;
    }

    @CrossOrigin(origins = "http://localhost:3000")
    @PostMapping(path =  "/maze")
    public Solution Test(@RequestBody Test test) {
       return solver.solve(test);
    }

}

Even though i could define a second constructor, i didn't find any way to call it with my Object.

  • 1
    Not sure what is your problem, can you give more information? Is your problem is about how to inject Solveable object in your controller? Or do you need to have more information about how to pass params to your query? Also with stack trace it can be more easy to help you. – EFOE Oct 12 '19 at 18:07
  • Do I understand correctly that you want a parameter that would configure your controller instance, rather than a request parameter? If so, have you tried to inject your parameter with `@Value` for example? – Cos64 Oct 12 '19 at 18:07
  • I want to specify a Object, that the RESTController will use, if a REST API Request occurs. This Object should be created f.e. in the Main in the Backend, where it should get passed to the RESTController. Since Solvable is an interface with the method solve(), it should be possible to use different Types of Objects that implement that interface. –  Oct 12 '19 at 18:10
  • You could try to make your `Solvable` implementation a bean (for example, annotate it with `@Service`, and then inject it into the controller (with `@Autowired` as a field). With Spring, one usually does not use `new` to create objects. – Cos64 Oct 12 '19 at 18:15
  • @Cos64 That is more what i was thinking of, but I am not sure, if it is possible to do that with an interface. –  Oct 12 '19 at 18:18
  • Yes it is possible, and it is even recommended to use interfaces. You declare a field of the *interface* type, you mark it as `@Autowired`, and the *concrete* type marked as `@Service` gets injected. – Cos64 Oct 12 '19 at 18:20

4 Answers4

2

Use @RequestParam annotation to pass an argument

null_user
  • 21
  • 2
1

You can pass parameter with @RequestParam annotation like this:

@CrossOrigin(origins = "http://localhost:3000")
@PostMapping(path =  "/maze")
public Solution Test(@RequestParam("paramName") String param, @RequestBody Test test) {
   return solver.solve(test);
}

And you can put it with http request:

http://localhost:3000/maze?paramName=someValue

Assuming that you have POST request, there may be different ways to build this request, depending on the API testing tools you use.

ivanjermakov
  • 1,131
  • 13
  • 24
  • Where do i pass that Parameter from? The Test-Method gets implicitly called when a request on the REST API occurs. –  Oct 12 '19 at 18:05
  • Maybe i didn't clarify enough: I want to pass an object from my backend to the RESTController, not trough the REST API. –  Oct 12 '19 at 18:07
  • @Tobias you always can call it as usual method: `connectorService.Test(param, test);` – ivanjermakov Oct 12 '19 at 18:09
  • I don't have the reference to the connectorService Object, because Spring creates this implicitly. –  Oct 12 '19 at 18:14
  • @Tobias see Dependency Injection and `@Autowired` annotation: https://stackoverflow.com/questions/19414734/understanding-spring-autowired-usage – ivanjermakov Oct 12 '19 at 18:15
0

Yo can create a Bean configuration file to initialize your objects like:

@Configuration
@ComponentScan("com.xxx.xxx") // the base package you want to scan
public class Config {

    @Bean
    //where Solveable is a class and is annotated with an Spring's annotation
    public Solveable solveable() {
        return new Solveable();
    }

}

And use the @Autowired annotation to inject the object in:

@Autowired    
public ConnectorService (Solveable solveable) {
      this.solver = solveable;
}

This last block will initialize or pass(what you want) the object to the ConnectorService class.

0

@RestController follows the same rules for dependency injection as any other @Component in Spring framework. If you have a single constructor, Spring will try to „inject” the parameters while instantiating the controller.

You need to register your dependency as a Spring bean.

It seems that you are new to Spring and you are starting with advanced topics like Spring Boot and rest controllers. Please find some time to read about the basics.

Mateusz Stefek
  • 3,478
  • 2
  • 23
  • 28