春季 REST 中的子资源

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

我必须从MessageResource调用RepositResource。

我想要单独的 MessageResources 和 CommentResources。

我正在做这样的事情:

消息资源.java

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

    MessageService messageService = new MessageService();

    @RequestMapping(value = "/{messageId}/comments")
    public CommentResource getCommentResource() {
        return new 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";
    }
}

我想要

http://localhost:8080/messages/1/comments/abc

返回“这是测试注释”。

任何想法??

PS:简单来说,我想知道在JAX-RS sub-resourcespring-rest


答案 1

在Spring boot中,我们可以使用Spring Concept实现jax-RS子资源概念@Autowired。创建子资源类的对象,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.

答案 2

您的网址(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;
    }
}

我不完全确定您希望在MessageResource类中做什么,但这个想法就在那里。

Rest - 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);
 }

类名

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

在注释后编辑 - 拆分控制器

你可以从字面上拆分控制器(更接近你所拥有的):

@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;
    }
}