创建与JAX-RS Location头响应 [英] Create Response with Location header in JAX-RS

查看:236
本文介绍了创建与JAX-RS Location头响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上课自动生成与来自实体REST风格的模板,具有CRUD功能的NetBeans(与邮政注释,GET,PUT,DELETE)。我有一个的创建的方法,它从前端插入一个实体之后,我想的创建的更新响应的问题,所以,我的观点会自动将(或异步,如果。这是正确的术语)反映加实体

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.

我碰到这个(实例)的代码行,但写在C#(其中我一无所知):

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);



在Java中使用JAX-RS,反正是有得到当前的HttpContext就像在C#和操纵头?

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

最近我来到的是

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



而这一次肯定是行不通的。看来我需要建立响应之前得到当前HttpContext。

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

感谢您的帮助。

推荐答案

我觉得你的意思是做这样的事情 Response.created(createdURI).build()。这将创建一个 201创建状态的响应,用 createdUri 作为位置标头值。通常这是用的POST完成。在客户端,你可以叫 Response.getLocation()将返回新的URI。

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.

响应API

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

公共抽象URI的getLocation() - 返回位置URI,如果不存在,否则返回null

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

请有关位置您指定的创建方法:

新资源的URI。如果相对URI是通过解决其相对于请求URI提供它将会被转换成绝对URI。

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.

如果你不不想依靠静态资源的路径,你可以得到从的 UriInfo 类。你可以不喜欢

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

这会造成位置 ... /客户/ 1 (或任何客户ID 是),并把它作为响应头

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

请注意,如果你想与响应一起发送的实体,你可以附加的 实体(对象)的方法>在 Response.ReponseBuilder

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

这篇关于创建与JAX-RS Location头响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆