Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sub-Resources in spring REST

I'm trying to build messanger app.

I've to call CommentResource from MessageResource.

I want separate MessageResources and CommentResources.

I'm doing something like this :

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

I want

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

to return "this is test comment".

Any Idea??

PS: In a simple word, I want to know JAX-RS sub-resource equivalent implementation in spring-rest

like image 844
prranay Avatar asked Oct 30 '16 12:10

prranay


People also ask

What is a sub resource?

Noun. subresource (plural subresources) (chiefly computing) A resource making up part of a larger resource.

What does @RestController do in spring?

Spring 4.0 introduced the @RestController annotation in order to simplify the creation of RESTful web services. It's a convenient annotation that combines @Controller and @ResponseBody, which eliminates the need to annotate every request handling method of the controller class with the @ResponseBody annotation.

What is difference between spring REST and Jersey?

Jersey is the JAX-RS API example implementation provided by Sun, while Spring REST is of course Spring's implementation of the same API/JSRs. The major difference is that Spring REST easily integrates into other Spring APIs (if you wish) such as Spring Data Rest.


1 Answers

In Spring boot, we can implement JAX-RS subresource concept using @Autowired Spring Concept. Create an object of your child resource class and Spring will initialize at runtime and return that object. Don't create an Object manually. like: Above mention scenario

 - 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.
like image 153
Tapan Avatar answered Sep 22 '22 18:09

Tapan