Spring Data Rest 控制器:@BasePathAwareController、@RepositoryRestController、@Controller 和@RestController 的行为和用法

Spring Data Rest controllers: behaviour and usage of @BasePathAwareController, @RepositoryRestController, @Controller and @RestController

我正在尝试了解 Spring 数据剩余控制器的确切行为。

我做了一个简单的实现来测试4种带注释的控制器:@BasePathAwareController@RepositoryRestController@RestController@Controller

控制器在存储库中有实体 "author" 的映射。

这是控制器:

@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 调用这两个 URLS:

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.

尽管有 控制台上的正确处理消息。

其他两个注释(@BasePathAwareController@RepositoryRestController)什么都不做。

总结:

如能对每种 @Controller 的行为和用法进行任何类型的说明,我们将不胜感激。

谢谢。

因为你正在使用 REST API 你需要使用 @RestController

如需进一步阅读,请查看 spring docs

我找到了让所有带注释的控制器工作的方法,我在这里分享。

@BasePathAwareController@RepositoryRestController 必须有一个 @RequestMapping 在 class 级别 :

@RepositoryRestController
//@BasePathAwareController
@RequestMapping(value="/authors/mycontroller")
public class MyController {

    @RequestMapping(value="/test")
    public void handleRequest(){
        //...
    }
}

如果没有 class 级别的映射,则不会处理两个控制器

这是因为在我之前的测试中从未调用过这些方法。

class级别的映射可以以“/”开头,也可以不以“/”开头,这两种情况都有效。

此外,我注意到在配置上下文中添加 <mvc:default-servlet-handler/>,警告 "No mapping found for HTTP request with URI" 消失了。