users@jersey.java.net

POST method on subresource

From: George <george.news_at_gmx.net>
Date: Tue, 09 Feb 2010 12:49:55 +0100

Hi,

I would like to perform a post on a subresource but although defined in
code, the post is not exported.

I have /card/{id} and I want to make a post to /card/{id} as it will
add stuff to the id subresource. However I'm not able to do that. Only
put is exported.

POST /card/{id} --> /card/{id}/whatever and modify status of card


Code (I have remove some parts of the code):

public class CardResource {

        @Context
        private UriInfo context;

        /** Creates a new instance of CardResource */
        public CardResource(UriInfo context, String id) {
                this.context = context;
                this.card = CardsDB.getInstance().findById(id);
        }

        @GET
        @Produces("application/xml")
        public Card getXml() {
                return card;
        }

        @PUT
        @Consumes("application/xml")
        public void putXml(@PathParam("id") String id, String content) {
                // I don't use it
        }

        @DELETE
        public void delete(@PathParam("id") String id) {
        }

        @POST
        public void post(JAXBElement<Whatever> whatever) {
        }
}

public class CardsResource {
        
        @Context
        private UriInfo context;

        /** Creates a new instance of CardsResource */
        public CardsResource() {
        }

        @GET
        @Produces({"application/xml", "application/json"})
        public Cards getXml() {
                //TODO return proper representation object
                Cards a = new Cards();
                a.addAll(CardsDB.getInstance().getCards());

                return a;
        }

        @POST
        @Consumes({"application/xml", "application/json"})
        @Produces({"application/xml", "application/json"})
        public Response postXml(JAXBElement<Card> card) {
                CardsDB.getInstance().addCard(card.getValue());
                URI carUri = context.getAbsolutePathBuilder().
                        path(card.getValue().getId().toString()).
                        build();

                ResponseBuilder resp = Response.ok(card);
                resp.location(carUri);
                return resp.build();
                //return Response.created(carUri).build();
        }


        @Path("{id}")
        public CardResource getCardResource(@PathParam("id") String id) {
                return new CardResource(context, id);
        }
}