How do I get the JAX-RS @Path of a different resource during a POST?

前端 未结 4 1820
走了就别回头了
走了就别回头了 2021-02-02 15:09

I have two REST classes for a simple web service (Jersey and GlassFish) that involves user resources - one to operate on all users (e.g., a factory for @POSTing) and another on

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-02 15:56

    You can use UriBuilder.fromresource(), but this only works if the supplied Resource class is a root resource (this is clearly mentioned in the javadocs). I found a way to achieve this even if you are in a sub-resource class:

    @POST
    @Consumes({MediaType.APPLICATION-XML, MediaType.APPLICATION-JSON})
    public Response createUser(final User user, @Context UriInfo uriInfo) {
        // persist the user here
        URI uri = uriInfo.getAbsolutePathBuilder().path(user.getId()).build();
        return Response.created(uri).build();
    }
    

提交回复
热议问题