Ale*_*o C 12 spring spring-mvc spring-data-rest
我试图了解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");
}
}
Run Code Online (Sandbox Code Playgroud)
我正在测试4种方法,因为我对要使用的正确映射有一些疑问.
对于每个实验,我使用具有相同映射的不同控制器,只需取消注释该实验所需的注释.
我用HTTP GET调用这两个URL:
http://localhost:8080/myApp/api/mycontroller/test
http://localhost:8080/myApp/api/mycontroller/testslash
Run Code Online (Sandbox Code Playgroud)
这是我获得的结果@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
Run Code Online (Sandbox Code Playgroud)
这是使用的结果@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'
Run Code Online (Sandbox Code Playgroud)
这是我使用的结果@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"
Run Code Online (Sandbox Code Playgroud)
最后,这是使用的结果@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'
Run Code Online (Sandbox Code Playgroud)
我不明白发生了什么.
似乎提供预期结果的唯一注释是@RestController.
@Controller显然似乎工作,但我有一个HTTP状态404,并有一个不一致的URL消息/myApp/api/authors/mycontroller/authors/mycontroller/test,尽管在控制台上有正确的处理消息.
其他两个注释(@BasePathAwareController和@RepositoryRestController)什么都不做.
综上所述:
@BasePathAwareController:什么都不做@RepositoryRestController:什么都不做@Controller:奇怪的行为@RestController:完美的工作非常感谢关于各种@Controller的行为和用法的任何类型的澄清.
谢谢.
我找到了所有带注释的控制器的工作方式,我在这里分享.
@BasePathAwareController并且@RepositoryRestController 必须具有@RequestMapping班级水平:
@RepositoryRestController
//@BasePathAwareController
@RequestMapping(value="/authors/mycontroller")
public class MyController {
@RequestMapping(value="/test")
public void handleRequest(){
//...
}
}
Run Code Online (Sandbox Code Playgroud)
如果没有类级别的映射,则不会处理两个控制器.
这就是原因,因为在我之前的测试中,这些方法从未被调用过.
类级别的映射可以以"/"开头,但在两种情况下都适用.
此外,我注意到<mvc:default-servlet-handler/>在配置上下文中添加警告"没有找到HTTP请求与URI的映射"已消失.
| 归档时间: |
|
| 查看次数: |
4116 次 |
| 最近记录: |