简体   繁体   English

"春季启动休息控制器端点不起作用"

[英]Spring boot rest controller endpoint not working

Creating a simple Spring Boot application using Maven .使用Maven创建一个简单的Spring Boot应用程序。 I have given a value with RestController annotation, but it doesn't work.我用RestController注释给出了一个值,但它不起作用。 If I don't use the RestController 's value, it works.如果我不使用RestController的值,它可以工作。 I want to know, why it's not working and What's the use of value in @RestController ?我想知道,为什么它不起作用以及@RestController中的 value 有什么用?

http://localhost:9090/app/hello this gives error http://localhost:9090/app/hello这给出了错误

http://localhost:9090/hello this works fine http://localhost:9090/hello这工作正常

@RestController("/app") What's the purpose of "/app" this value inside @RestController annotation? @RestController("/app") @RestController注解中"/app"这个值的目的是什么?

PS: I know, I can use @RequestMapping("/app") on ScraperResource class. PS:我知道,我可以在 ScraperResource 类上使用@RequestMapping("/app")

@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.那是因为 RestController 中的“/app”与您的 URL mapping 无关,而是与 Spring 内部使用的“逻辑组件”名称有关

You should do this instead, if you want to prefix all your controller methods with /app (or just leave it out).如果您想用 /app 为所有控制器方法添加前缀(或将其省略),您应该这样做。

@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.没有@RestController Spring 不会知道这个类应该处理 HTTP 调用,所以它是一个需要的注解。

As per the Java Doc associated with the @RestController annotation, this is the meaning of the value that you are passing to it:根据与@RestController注释关联的 Java Doc,这是您传递给它的值的含义:

/**
     * 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.因此,它不会影响或影响您的端点可访问的 URL。 If you want to add a top-level mapping you can use the @RequestMapping("/app") on the class-level as you mentioned.如果你想添加一个顶级映射,你可以在类级别使用@RequestMapping("/app") ,正如你提到的。

The parameter in the @Controller<\/code> annotation allows you to name the Controller. @Controller<\/code>注解中的参数允许您命名控制器。 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.如果有多个相同类型的 bean,则可以将 bean 名称与@Qualifier<\/code>注解一起使用,以让 Spring 知道在自动装配期间要注入哪个组件。

From the documentation<\/a> :文档<\/a>中:

When a component is autodetected as part of the scanning process, its bean name is generated by the BeanNameGenerator strategy known to that scanner.当一个组件作为扫描过程的一部分被自动检测时,它的 bean 名称由该扫描器已知的 BeanNameGenerator 策略生成。 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.默认情况下,任何包含名称值的 Spring 原型注解(@Component、@Repository、@Service 和 @Controller)都会将该名称提供给相应的 bean 定义。

If such an annotation contains no name value or for any other detected component (such as those discovered by custom filters), the default bean name generator returns the uncapitalized non-qualified class name.如果此类注释不包含名称值或任何其他检测到的组件(例如由自定义过滤器发现的组件),则默认 bean 名称生成器将返回未大写的非限定类名称。

<\/blockquote>

Read more about autowire disambiguation, and how to use the component name with @Qualifier<\/code> here<\/a> .在此处<\/a>阅读有关自动装配消歧的更多信息,以及如何将组件名称与@Qualifier<\/code>一起使用。

By default, Spring resolves @Autowired entries by type.默认情况下,Spring 按类型解析 @Autowired 条目。 If more than one bean of the same type is available in the container, the framework will throw a fatal exception.如果容器中有多个相同类型的 bean 可用,框架将抛出​​一个致命异常。

To resolve this conflict, we need to tell Spring explicitly which bean we want to inject.为了解决这个冲突,我们需要明确地告诉 Spring 我们要注入哪个 bean。

When there are multiple beans of the same type, it's a good idea to use @Qualifier to avoid ambiguity.当有多个相同类型的 bean 时,最好使用 @Qualifier 来避免歧义。

Please note that the value of the @Qualifier annotation matches with the name declared in the @Component annotation of our FooFormatter implementation.请注意,@Qualifier 注释的值与我们的 FooFormatter 实现的 @Component 注释中声明的名称匹配。

<\/blockquote>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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