Spring Data Rest控制器:@ BasePathAwareController,@ RepositoryRestController,@ Controller和@RestController的行为和用法
问题内容:
我正在尝试了解Spring Data Rest Controllers的确切行为。
我做了一个简单的实现,测试注释控制器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
/myApp/api/authors/mycontroller/authors/mycontroller/test
,尽管控制台上有正确的处理消息,但有一条URL不一致的消息。
其他两个注释(@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(){
//...
}
}
如果没有类级别的映射,则不会处理两个控制器 。
这是因为在我以前的测试中从未调用过这些方法。
类级别的映射可以以“ /”开头,也可以不以“ /”开头,在两种情况下均有效。
此外,我注意到<mvc:default-servlet-handler/>
在配置上下文中添加的警告 “找不到具有URI的HTTP请求的映射”
消失了。