kul*_*sin 2 java rest spring spring-boot
创建一个简单Spring Boot使用的应用程序Maven。我给了一个带RestController注释的值,但它不起作用。如果我不使用RestController's 值,它会起作用。我想知道,为什么它不起作用以及 value 在 中有@RestController什么用?
http://localhost:9090/app/hello 这给出了错误
http://localhost:9090/hello 这工作正常
@RestController("/app")注释中的"/app"这个值的目的是什么@RestController?
PS:我知道,我可以@RequestMapping("/app")在 ScraperResource 类上使用。
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
@RestController("/app")
public class ScraperResource {
@GetMapping("hello")
public String testController() {
return "Hello";
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序属性
server.port=9090
Run Code Online (Sandbox Code Playgroud)
这是因为 RestController 中的“/app”与您的 URL mapping 无关,而是与Spring 内部使用的“逻辑组件”名称有关。
如果您想用 /app 为所有控制器方法添加前缀(或将其省略),您应该这样做。
@RestController
@RequestMapping("/app")
public class ScraperResource {
@GetMapping("hello")
public String testController() {
return "Hello";
}
}
Run Code Online (Sandbox Code Playgroud)
没有@RestController Spring 不会知道这个类应该处理 HTTP 调用,所以它是一个需要的注解。
| 归档时间: |
|
| 查看次数: |
3238 次 |
| 最近记录: |