Spring REST中的子资源

prr*_*nay 14 java rest spring-mvc spring-boot request-mapping

我正在尝试构建messanger应用程序.

我要从MessageResource调用CommentResource.

我想要单独的MessageResources和CommentResources.

我正在做这样的事情:

MessageResource.java

@RestController
@RequestMapping("/messages")
public class MessageResource {

    MessageService messageService = new MessageService();

    @RequestMapping(value = "/{messageId}/comments")
    public CommentResource getCommentResource() {
        return new CommentResource();
    }
}
Run Code Online (Sandbox Code Playgroud)

CommentResource.java

@RestController
@RequestMapping("/")
public class CommentResource {

    private CommentService commentService = new CommentService();

    @RequestMapping(method = RequestMethod.GET, value="/abc")
    public String test2() {
        return "this is test comment";
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要

HTTP://本地主机:8080 /消息/ 1 /评论/ ABC

返回"这是测试评论".

任何的想法??

PS:简单来说,我想知道JAX-RS sub-resource相应的实现spring-rest

Tap*_*pan 17

在 Spring boot 中,我们可以使用@Autowired Spring Concept实现 JAX-RS 子资源概念。创建子资源类的对象,Spring 将在运行时初始化并返回该对象。不要手动创建对象。像:上面提到的场景

 - MessageResource.java

@RestController
@RequestMapping("/messages")
public class MessageResource {

    MessageService messageService = new MessageService();
    @Autowired
    @Qualifier("comment")
    CommentResource comment;

    @RequestMapping(value = "/{messageId}/comments")
    public CommentResource getCommentResource() {
        return comment;
    }
}    

 - CommentResource.java

@RestController("comment")
@RequestMapping("/")
public class CommentResource {

    private CommentService commentService = new CommentService();

    @RequestMapping(method = RequestMethod.GET, value="/abc")
    public String test2() {
        return "this is test comment";
    }
}



Now it will work like sub-resource
http://localhost:8080/messages/1/comments/abc

You can send any type of request.
Run Code Online (Sandbox Code Playgroud)

  • 你是如何运行它的?它需要一些配置吗?在当前版本的 spring boot (2.2.4.RELEASE) 中,它将 CommentResource 视为要序列化的响应值。如果我添加一个带有 getter 的字段,我会得到它的 json 表示形式。在调用 GET http://localhost:8080/messages/1/comments/abc 时,我得到 404。 (3认同)
  • 伙计,我不知道如何理解这一点。首先,“public CommentResource getCommentResource()”返回 CommentResource,但不会将 messageId 上下文注入其中或以任何方式使用它..所以我不确定这是否是答案中的错误或我的知识差距- 如果能得到澄清,我们将不胜感激。此外,评论资源具有与“评论”不同的路径“评论” - 例如单数与复数,所以我不确定这是否是故意的 - 我认为这是一个错误。感谢您的任何更正或回复。 (2认同)

ale*_*xbt 7

您的网址(http:// localhost:8080/messages/1/comments/abc)表明评论嵌套在邮件中.您的控制器应如下所示:

@RestController
@RequestMapping("/messages")
public class MessageResource {

    @RequestMapping(value = "/{messageId}")
    public String getCommentResource(@PathVariable("messageId") String messageId) {
        //test
        return messageId;
    }

    @RequestMapping(value = "/{messageId}/comments/{commentsContent}")
    public String getCommentResource(
                      @PathVariable("messageId") String messageId, 
                      @PathVariable("commentsContent") String commentsContent) {
        //test
        return messageId + "/" + commentsContent;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不完全确定你想在MessageResource类中做什么,但想法就在那里.

休息 - HTTP方法

目前,这些用途是Get请求.但是,您应该考虑使用适当的Http方法:

  • 获取:阅读资源
  • 发布:创建资源
  • 放:更新
  • 删除:删除:)

看看这个:http://www.restapitutorial.com/lessons/httpmethods.html

帖子示例:

@RequestMapping(method=RequestMethod.POST, value = "/{messageId}/comments/{commentsContent}")
    public ResponseEntity<String> getCommentResource(
                                  @PathVariable("messageId") String messageId, 
                                  @RequestBody Comment comment) {
        //fetch the message associated with messageId
        //add the comment to the message
        //return success
        return new ResponseEntity<String>(HttpStatus.OK);
 }
Run Code Online (Sandbox Code Playgroud)

班级名称

另外,我个人会将这些类重命名为MessageController和CommentController.

注释后编辑 - 拆分控制器

你可以简单地拆分控制器(更接近你所拥有的):

@RestController
@RequestMapping("/messages")
public class MessageResource {

    @RequestMapping(value = "/{messageId}")
    public String getCommentResource(@PathVariable("messageId") String messageId) {
        //test
        return messageId;
    }
}

@RestController
@RequestMapping("/messages")
public class CommentResource {

    @RequestMapping(value = "/{messageId}/comments/{commentsContent}")
    public String getCommentResource(
                      @PathVariable("messageId") String messageId, 
                      @PathVariable("commentsContent") String commentsContent) {
        //test
        return messageId + "/" + commentsContent;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

您正在寻找的内容在JerseyJAX-RS实现中得到支持,并被称为. 在构建本质上嵌套的大型 API 时,子资源是一个非常有用的功能。Sub-Resources

Spring Boot 默认的 rest 实现不是一个JAX-RSbut SpringMVC。虽然可以在 Spring Boot 中使用 Jersey,但尝试设置它有点复杂,并且在社区中似乎没有得到很好的使用/支持。

顺便说一句DropWizard很棒!