Java Spring引导rest控制器终结点不工作

Java Spring引导rest控制器终结点不工作,java,spring,spring-boot,rest,Java,Spring,Spring Boot,Rest,使用Maven创建一个简单的Spring引导应用程序。我已经用RestController注释给出了一个值,但它不起作用。如果我不使用RestController的值,它就会工作。我想知道为什么它不起作用,@RestController中的值有什么用途 http://localhost:9090/app/hello 这就产生了错误 http://localhost:9090/hello 这个很好用 @RestController/app在@RestController注释中/app此值的用途是什

使用Maven创建一个简单的Spring引导应用程序。我已经用RestController注释给出了一个值,但它不起作用。如果我不使用RestController的值,它就会工作。我想知道为什么它不起作用,@RestController中的值有什么用途

http://localhost:9090/app/hello 这就产生了错误

http://localhost:9090/hello 这个很好用

@RestController/app在@RestController注释中/app此值的用途是什么

我知道,我可以在ScraperResource类上使用@RequestMapping/app

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
应用程序属性

server.port=9090

这是因为RestController中的/app与URL映射无关,而是与Spring内部使用的逻辑组件名称有关

如果您想在所有控制器方法前面加上/app,或者干脆不加/app,那么您应该这样做

@RestController
@RequestMapping("/app")
public class ScraperResource {

    @GetMapping("hello")
    public String testController() {
        return "Hello";
    }
}

如果没有@RestController,Spring将不知道该类应该处理HTTP调用,因此它是必需的注释。

这是因为RestController中的/app与URL映射无关,而是与Spring内部使用的逻辑组件名称有关

如果您想在所有控制器方法前面加上/app,或者干脆不加/app,那么您应该这样做

@RestController
@RequestMapping("/app")
public class ScraperResource {

    @GetMapping("hello")
    public String testController() {
        return "Hello";
    }
}

如果没有@RestController,Spring将不知道该类应该处理HTTP调用,因此它是一个必需的注释。

根据与@RestController注释相关联的Java文档,这是您传递给它的值的含义:

/**
     * 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 "";

因此,它不会影响或影响端点可访问的URL。如果要添加顶级映射,可以在类级别上使用@RequestMapping/app,如您所述。

根据与@RestController注释关联的Java文档,这是您传递给它的值的含义:

/**
     * 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 "";
因此,它不会影响或影响端点可访问的URL。如果要添加顶级映射,可以在类级别上使用@RequestMapping/app,如前所述