问题描述
我正在努力实现以下目标。
I am trying to achieve the following.
从请求中读取自定义标头及其值:
Read a custom header and its value from Request:
name: username
现在,作出回应,我想在HTTP响应中返回相同的头 name:value
对。
Now, on response, I would like to return the same header name:value
pair in HTTP response.
我正在使用Jersey 2.0实现的JAX- RS webservice。
I am using Jersey 2.0 implementation of JAX-RS webservice.
当我发送请求URL Http:// localhost / test /
时,请求标头是也通过了(暂时,虽然Firefox插件 - 硬编码)。
When I send the request URL Http://localhost/test/
, the request headers are also passed (for the time being, though Firefox plugin - hardcoding them).
收到该URL的请求后,将调用以下方法:
On receiving the request for that URL, the following method is invoked:
@GET
@Produces(MediaType.APPLICATION_JSON)
public UserClass getValues(@Context HttpHeaders header) {
MultivaluedMap<String, String> headerParams = header.getRequestHeaders();
String userKey = "name";
headerParams.get(userKey);
// ...
return user_object;
}
我怎样才能做到这一点?任何指针都会很棒!
How may I achieve this? Any pointers would be great!
推荐答案
只需注入 @Context HttpServletResponse响应
作为方法论证。更改该标题
Just inject a @Context HttpServletResponse response
as a method argument. Change the headers on that
@Produces(MediaType.APPLICATION_JSON)
public UserClass getValues(@Context HttpHeaders header, @Context HttpServletResponse response) {
response.setHeader("yourheadername", "yourheadervalue");
...
}
这篇关于自定义响应头泽西/ Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!