【发布时间】:2016-07-27 08:14:09
【问题描述】:
我正在尝试了解 Spring Data Rest Controller 的确切行为。
我做了一个简单的实现来测试 4 种带注释的控制器:@BasePathAwareController、@RepositoryRestController、@RestController、@Controller
控制器在存储库中有一个实体“作者”的映射。
这是控制器:
@BasePathAwareController
//@RepositoryRestController
//@RestController
//@Controller
public class MyController {
@RequestMapping(value="authors/mycontroller/test")
public void handleRequest(){
System.out.println("handleRequest of class MyController");
}
@RequestMapping(value="/authors/mycontroller/testslash")
public void handleSlashRequest(){
System.out.println("handleSlashRequest of class MyController");
}
@RequestMapping(value="api/authors/mycontroller/test")
public void handleApiRequest(){
System.out.println("handleApiRequest of class MyController");
}
@RequestMapping(value="/api/authors/mycontroller/testslash")
public void handleSlashApiRequest(){
System.out.println("handleSlashApiRequest of class MyController");
}
}
我正在测试 4 种方法,因为我对要使用的正确映射有一些疑问。
对于每个实验,我都使用具有相同映射的不同控制器,只需取消该实验所需的注释即可。
我正在使用 HTTP GET 调用这两个 URL:
http://localhost:8080/myApp/api/mycontroller/test
http://localhost:8080/myApp/api/mycontroller/testslash
这是我使用@BasePathAwareController得到的结果:
http://localhost:8080/myApp/api/mycontroller/test
White page, No errors, No print on console
http://localhost:8080/myApp/api/mycontroller/testslash
White page, No errors, No print on console
这是使用@RepositoryRestController的结果:
http://localhost:8080/myApp/api/mycontroller/test
White page, No errors, No print on console
http://localhost:8080/myApp/api/mycontroller/testslash
White page, and this message on the console (only for the first HTTP GET on this url):
jul 27, 2016 9:23:57 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myApp/api/authors/mycontroller/testslash] in DispatcherServlet with name 'rest'
如果我使用@RestController,这就是结果:
http://localhost:8080/myApp/api/mycontroller/test
White page, in console: "handleRequest of class MyController"
http://localhost:8080/myApp/api/mycontroller/testslash
White page, in console: "handleSlashRequest of class MyController"
最后,这是使用@Controller的结果:
http://localhost:8080/myApp/api/mycontroller/test
HTTP STATUS 404, in console: "handleRequest of class MyController"
http://localhost:8080/myApp/api/mycontroller/testslash
HTTP STATUS 404, in console: "handleSlashRequest of class MyController"
For both urls I have this warning:
jul 27, 2016 9:28:11 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myApp/api/authors/mycontroller/authors/mycontroller/test] in DispatcherServlet with name 'rest'
我不明白发生了什么。
似乎提供预期结果的唯一注释是@RestController。
@Controller 似乎可以正常工作,但我的 HTTP 状态为 404,并且有一条消息的 URL 不一致 /myApp/api/authors/mycontroller/authors/mycontroller/test,尽管有
控制台上的正确处理消息。
另外两个注解(@BasePathAwareController 和 @RepositoryRestController)什么都不做。
总结:
-
@BasePathAwareController: 什么都不做 -
@RepositoryRestController: 什么都不做 -
@Controller: 奇怪的行为 -
@RestController:完美运行
非常感谢您对每种@Controller 的行为和用法进行任何类型的说明。
谢谢。
【问题讨论】:
标签: spring spring-mvc spring-data-rest