Spring Data Rest控制器:@ BasePathAwareController,@ RepositoryRestController,@ Controller和@RestController的行为和用法
我试图了解Spring Data Rest控制器的确切行为.
I'm trying to understand the exact behaviour of the Spring Data Rest Controllers.
我已经做了一个简单的实现来测试4种带注释的控制器:@BasePathAwareController
,@RepositoryRestController
,@RestController
,@Controller
I have made a simple implementation to test 4 kinds of annotated controllers: @BasePathAwareController
, @RepositoryRestController
, @RestController
, @Controller
控制器在存储库中具有实体作者"的映射.
The controller has a mapping for an entity "author" in the repository.
这是控制器:
@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种方法,因为我对要使用的正确映射有一些疑问.
I'm testing 4 methods because I have some doubts about the right mapping to be used.
对于每个实验,我都使用具有相同映射的不同控制器,只是对该实验所需的注释进行了注释.
For every experiment, I use a different controller with the sames mappings, simply decommenting the annotation that I need for that experiment.
我正在使用HTTP GET调用这两个URL:
I'm calling these two URLS with HTTP GET:
http://localhost:8080/myApp/api/mycontroller/test
http://localhost:8080/myApp/api/mycontroller/testslash
这是我使用@BasePathAwareController
获得的结果:
This is the result that I obtain using @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
的结果:
Finally, this is the result using @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'
我不知道发生了什么事.
I don't understand what's going on.
唯一可以提供预期结果的注释是@RestController
.
The only annotation that seems to provide the expected result is the @RestController
.
@Controller
似乎可以正常工作,但是我有一个HTTP状态404,并且有一条消息,URL /myApp/api/authors/mycontroller/authors/mycontroller/test
不一致,尽管有
控制台上正确的处理消息.
@Controller
apparently seems to work, but I have an HTTP status 404 and there is a message with an inconsistent URL /myApp/api/authors/mycontroller/authors/mycontroller/test
, despite there is
the correct handling message on the console.
其他两个注释(@BasePathAwareController
和@RepositoryRestController
)什么也不做.
The other two annotations (@BasePathAwareController
and @RepositoryRestController
) don't do nothing.
总结:
-
@BasePathAwareController
:什么都不做 -
@RepositoryRestController
:什么也不做 -
@Controller
:奇怪的行为 -
@RestController
:完美运行
-
@BasePathAwareController
: doesn't do nothing -
@RepositoryRestController
: doesn't do nothing -
@Controller
: strange behaviour -
@RestController
: works perfectly
对于每种@Controller的行为和用法的任何类型的澄清,将不胜感激.
It would be greatly appreciated any type of clarification about the behaviour and the usage of every kind of @Controller.
谢谢.
我已经找到了使所有带注释的控制器工作的方法,我在这里分享了它.
I have found the way to make work all the annotated controllers, and I share it here.
@BasePathAwareController
和@RepositoryRestController
在班级必须具有@RequestMapping
:
@BasePathAwareController
and @RepositoryRestController
must have a @RequestMapping
at class level:
@RepositoryRestController
//@BasePathAwareController
@RequestMapping(value="/authors/mycontroller")
public class MyController {
@RequestMapping(value="/test")
public void handleRequest(){
//...
}
}
如果没有类级别的映射,则不会处理两个控制器.
这就是原因,因为在我以前的测试中,从未调用过这些方法.
That's the reason because in my previous tests the methods were never invoked.
类级别的映射可以以"/"开头,也可以不以"/"开头.
The mapping at class level can starts with "/" or not, it works in both cases.
此外,我注意到在配置上下文中添加<mvc:default-servlet-handler/>
时,警告找不到具有URI的HTTP请求的映射" 消失了.
Furthermore, I have noticed that adding <mvc:default-servlet-handler/>
in the configuration context, the warning "No mapping found for HTTP request with URI" is vanished.