简体   繁体   中英

Spring boot rest controller endpoint not working

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?

PS: 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

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.

As per the Java Doc associated with the @RestController annotation, this is the meaning of the value that you are passing to it:

/**
     * The value may indicate a suggestion for a logical component name,
     * to be turned into a Spring bean in case of an autodetected component.
     * @return the suggested component name, if any (or empty String otherwise)
     * @since 4.0.1
     */
    @AliasFor(annotation = Controller.class)
    String value() default "";

As such, it does not influence or affect what URL your endpoint is accessible with. If you want to add a top-level mapping you can use the @RequestMapping("/app") on the class-level as you mentioned.

The parameter in the @Controller<\/code> annotation allows you to name the Controller. In cases where there are multiple beans of the same type, the bean name can be used along with the @Qualifier<\/code> annotation to let Spring know which component to inject during autowiring.

By default, any Spring stereotype annotation (@Component, @Repository, @Service, and @Controller) that contains a name value thereby provides that name to the corresponding bean definition.

If more than one bean of the same type is available in the container, the framework will throw a fatal exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM