我正在尝试了解 Spring Data Rest Controller 的确切行为。
我做了一个简单的实现来测试4种带注释的 Controller :@BasePathAwareController
, @RepositoryRestController
, @RestController
, @Controller
Controller 在存储库中有一个实体“作者”的映射。
这是 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 种方法,因为我对要使用的正确映射有一些疑问。
对于每个实验,我使用具有相同映射的不同 Controller ,只需将实验所需的注释注释掉。
我正在使用 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 的行为和用法进行任何类型的说明。
谢谢。
最佳答案
我已经找到了让所有带注释的 Controller 工作的方法,我在这里分享一下。
@BasePathAwareController
和 @RepositoryRestController
必须有 @RequestMapping
在类(class)级别:
@RepositoryRestController
//@BasePathAwareController
@RequestMapping(value="/authors/mycontroller")
public class MyController {
@RequestMapping(value="/test")
public void handleRequest(){
//...
}
}
如果没有类级别的映射,则不会处理两个 Controller 。
这就是原因,因为在我之前的测试中,这些方法从未被调用过。
类级别的映射可以以“/”开头,也可以不以“/”开头,在这两种情况下都有效。
此外,我注意到添加 <mvc:default-servlet-handler/>
在配置上下文中,警告 “No mapping found for HTTP request with URI” 消失了。
关于Spring Data Rest Controller : behaviour and usage of @BasePathAwareController, @RepositoryRestController、@Controller 和 @RestController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38607421/