Issue
Creating a simple Spring Boot
application using Maven
. I have given a value with RestController
annotation, but it doesn't work. If I don't use the RestController
's value, it works. I want to know, why it's not working and What's the use of value in @RestController
?
http://localhost:9090/app/hello
this gives error
http://localhost:9090/hello
this works fine
@RestController("/app")
What's the purpose of "/app"
this value inside @RestController
annotation?
P.S: I know, I can use @RequestMapping("/app")
on ScraperResource class.
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
@RestController("/app")
public class ScraperResource {
@GetMapping("hello")
public String testController() {
return "Hello";
}
}
application.properties
server.port=9090
Solution
That is because the "/app" inside your RestController has nothing to do with your URL mapping, but rather with a "logical component" name being used internally Spring.
You should do this instead, if you want to prefix all your controller methods with /app (or just leave it out).
@RestController
@RequestMapping("/app")
public class ScraperResource {
@GetMapping("hello")
public String testController() {
return "Hello";
}
}
Without @RestController Spring won't know that this class should handle HTTP calls, so it is a needed annotation.
Answered By - Marco Behler
Answer Checked By - Cary Denson (JavaFixing Admin)