[英]Custom response header Jersey/Java
I am trying to achieve the following.我正在努力实现以下目标。
Read a custom header and its value from Request:从请求中读取自定义 header 及其值:
name: username
Now, on response, I would like to return the same header name:value
pair in HTTP response.现在,根据响应,我想在 HTTP 响应中返回相同的 header name:value
对。
I am using Jersey 2.0 implementation of JAX-RS webservice.我正在使用 JAX-RS web 服务的 Jersey 2.0 实现。
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 Http://localhost/test/
时,请求标头也被传递(暂时,虽然 Firefox 插件 - 对它们进行硬编码)。
On receiving the request for that URL, the following method is invoked:在收到对 URL 的请求后,将调用以下方法:
@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!任何指针都会很棒!
I think using javax.ws.rs.core.Response
is more elegant and it is a part of Jersey.我认为使用javax.ws.rs.core.Response
更优雅,它是 Jersey 的一部分。 Just to extend previous answer, here is a simple example:只是为了扩展以前的答案,这是一个简单的例子:
@GET
@Produces({ MediaType.APPLICATION_JSON })
@Path("/values")
public Response getValues(String body) {
//Prepare your entity
Response response = Response.status(200).
entity(yourEntity).
header("yourHeaderName", "yourHeaderValue").build();
return response;
}
Just inject a @Context HttpServletResponse response
as a method argument.只需注入一个@Context HttpServletResponse response
作为方法参数。 Change the headers on that更改标题
@Produces(MediaType.APPLICATION_JSON)
public UserClass getValues(@Context HttpHeaders header, @Context HttpServletResponse response) {
response.setHeader("yourheadername", "yourheadervalue");
...
}
Return a Response
(a class from JAX-RS) with UserClass
as the entity.返回一个以UserClass
作为实体的Response
(来自 JAX-RS 的一个类)。 On the Response
you can set HTTP headers.在Response
您可以设置 HTTP 标头。
I found that the HttpServletResponse approach did not work.我发现 HttpServletResponse 方法不起作用。 Instead, we installed a custom response filter:相反,我们安装了一个自定义响应过滤器:
@Provider
public class MyFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext ctx) throws IOException {
ArrayList<Object> valueList = new ArrayList<>();
valueList.add("my-header-value");
ctx.getHeaders().put("My-header", valueList);
...
}
}
And the web app:和 web 应用程序:
public class MyApp extends Application {
static Set<Object> singletons = new LinkedHashSet<>();
public static void setSingletons(Set<Object> singletons) {
MyApp.singletons = singletons;
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
Set<Object> singletons = new LinkedHashSet<>();
singletons.add(new MyFilter());
MyApp.setSingletons(singletons);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.