Spring boot rest controller endpoint not working

kulsin :

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?

@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

P.S: I know, I can use @RequestMapping("/app") on ScraperResource class.

Marco Behler :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291602&siteId=1