Spring REST中的子资源

时间:2016-10-30 12:03:48

标签: 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();
    }
}

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-resource中的spring-rest等效实现

7 个答案:

答案 0 :(得分: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;
    }
}

我不完全确定你想在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);
 }

班级名称

另外,我个人会将这些类重命名为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;
    }
}

答案 1 :(得分:3)

JAX-RS实施支持您所寻找的内容,例如Jersey,并称为Sub-Resources。在构建大型嵌套的API时,子资源是一个非常有用的功能。

Spring Boot默认休息实现不是JAX-RS而是SpringMVC。虽然可以在Spring Boot中使用Jersey,但它有点涉及尝试设置它,并且在社区中似乎没有得到很好的使用/支持。

另一方面,DropWizard太棒了!

答案 2 :(得分:3)

在Spring引导中,我们可以使用 @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.

答案 3 :(得分:1)

我也被迫从JAX-RS迁移到Spring-MVC。

我仍然在寻找一种优雅的方式,就像我使用JAX-RS一样。

我正在分享我尝试过的东西。

@RestController
@RequestMapping("parents")
public class ParentsController {

    @RequestMapping(method = RequestMethod.GET,
                    produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<List<Parent>> read() {
    }

    @RequestMapping(method = RequestMethod.GET,
                    path = "/{id:\\d+}",
                    produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<Parent> read(@PathVariable("id") final long id) {
    }
}

ChildrenController

@RestController
@RequestMapping("/parents/{parentId:\\d+}/children")
public class ChildrenController {

    @RequestMapping(method = RequestMethod.GET,
                    produces = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseBody
    public List<Child> read(@PathVariable("parentId") final long parentId) {
    }

    @RequestMapping(method = RequestMethod.GET, path = "/{id:\\d+}",
                    produces = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseBody
    public Child read(@PathVariable("parentId") final long parentId,
                     @PathVariable("id") final long id) {
    }
}

我发现了两个问题,

@PathVariable不适用于字段。

我不能@PathVariable("parentId") private long parentId;

ChildrenController

的多重映射没有自由意志

JAX-RS的一个美妙之处在于我们可以将ChildrenController映射到不同的路径,就像ChildrenController有一个类级别@Path一样。

@Path("/children");
public class ChildrenResource {
}

@Path("/parents")
public class ParentsResource {

    @Path("/{id}/children")
    public ChildrenResource resourceChildren() {
    }
}


/children
/parents/{id}/children

答案 4 :(得分:1)

只需创建两个类并使用常量来引用子资源和父资源。这有助于在两个类之间建立链接,并使开发人员了解它们之间的关系。

所以:

@RequestMapping(value= MessageController.URL)
public class MessageController {
    public static final String URL= "/messages";
}

@RequestMapping(value = MessageController.URL + "/{idMessage}/comments")
public class CommentController {

}

您还可以将控制器拆分到不同的包中,在包组织中也显示此层次结构:

com.company.web.message.MessageController
com.company.web.message.comment.CommentController 

答案 5 :(得分:0)

MessagesController.java

@RestController
@RequestMapping(value = "/messages")
public class MessageController {

    @Autowired
    private MessagesService messageService;

}

CommentController.java

@RestController
@RequestMapping("/messages/{messageId}/comments")
public class CommentController {


    @GetMapping
    public List<Comment> getComments(@PathVariable("messageId") Long messageId) {
        System.out.println("Get "+messageId);

        return null;
    }

}

答案 6 :(得分:0)

MessageResource.java

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

    MessageService messageService = new MessageService();

    // as usual messages related CRUD operations
}

CommentResource.java

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

    private CommentService commentService = new CommentService();

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