[英]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>中:
<\/blockquote>
Read more about autowire disambiguation, and how to use the component name with
@Qualifier<\/code>
here<\/a> .
在此处<\/a>阅读有关自动装配消歧的更多信息,以及如何将组件名称与
@Qualifier<\/code>一起使用。
<\/blockquote>"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.