Create Response with Location header in JAX-RS

青春壹個敷衍的年華 提交于 2020-01-09 09:27:46

问题


I have classes auto-generated in NetBeans with RESTful template from entities, with CRUD functions (annotated with POST, GET, PUT, DELETE). I have a problem with create method, which after inserting an entity from the frontend, I would like create to update a response so that my view will automatically (or asynchronously, if that's the right term) reflect the added entity.

I came across this (example) line of code but written in C# (of which I know nothing about):

HttpContext.Current.Response.AddHeader("Location", "api/tasks" +value.Id);

Using JAX-RS in Java, is there anyway to get the current HttpContext just like in C# and to manipulate the header?

The closest I came about is

Response.ok(entity).header("Location", "api/tasks" + value.Id);

and this one certainly is not working. It seems I need to get the current HttpContext before building the Response.

Thanks for your help.


回答1:


I think you mean to do something like Response.created(createdURI).build(). This will create a response with a 201 Created status, with the createdUri being the location header value. Normally this is done with POSTs. On the client side, you can call Response.getLocation() which will return the new URI.

From the Response API

  • public static Response.ResponseBuilder created(URI location) - Create a new ResponseBuilder for a created resource, set the location header using the supplied value.

  • public abstract URI getLocation() - returns the location URI, otherwise null if not present.

Keep in mind about the location you specify to the created method:

the URI of the new resource. If a relative URI is supplied it will be converted into an absolute URI by resolving it relative to the request URI.

If you don't want to rely on static resource paths, you could get the current uri path from the UriInfo class. You could do something like

@Path("/customers")
public class CustomerResource {
    @POST
    @Consumes(MediaType.APPLICATION_XML)
    public Response createCustomer(Customer customer, @Context UriInfo uriInfo) {
        int customerId = // create customer and get the resource id
        UriBuilder builder = uriInfo.getAbsolutePathBuilder();
        builder.path(Integer.toString(customerId));
        return Response.created(builder.build()).build();
    }
}

This would create the location .../customers/1 (or whatever the customerId is), and send it as the response header

Note if you want to send the entity along with the response, you can just attach the entity(Object) to the method chain of the Response.ReponseBuilder




回答2:


 @POST
public Response addMessage(Message message, @Context UriInfo uriInfo) throws URISyntaxException
{
    System.out.println(uriInfo.getAbsolutePath());

    Message newmessage = messageService.addMessage(message);

    String newid = String.valueOf(newmessage.getId()); //To get the id

    URI uri = uriInfo.getAbsolutePathBuilder().path(newid).build();

    return Response.created(uri).entity(newmessage).build();
}


来源:https://stackoverflow.com/questions/26092318/create-response-with-location-header-in-jax-rs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!