JAXRS响应通用标头
有什么方法可以将通用标题设置为JAXR中所有API路径的响应吗?
例如,我有这样的API:
@Path("/api/v1")
public class JaxRsConfig extends Application {
}
而且
@Path("/voucher")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class Voucher {
@POST
public Response add(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
@GET
public Response get(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
@GET
public Response list(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
}
:
@Path("/invoice")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class Invoice {
@POST
public Response add(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
@GET
public Response get(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
@GET
public Response list(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
}
我总是必须将此标头放在响应中。
JAXRS通常可以设置此标头的机制?
注意:我在Liberty应用程序服务器上使用Javaee-8
is there any way to set general header on response of all api paths in JaxRS ?
for example i have a api like this :
@Path("/api/v1")
public class JaxRsConfig extends Application {
}
and
@Path("/voucher")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class Voucher {
@POST
public Response add(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
@GET
public Response get(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
@GET
public Response list(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
}
and this:
@Path("/invoice")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class Invoice {
@POST
public Response add(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
@GET
public Response get(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
@GET
public Response list(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
}
I always have to put this header in the response .
JaxRs has any mechanism to set this header generally ?
Note: I use JavaEE-8 on Liberty Application Server
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用 WriterInterceptor 来添加此标头。
这里解释了一些很好的例子: https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-edition/content/en/part1/chapter12/reader_and_writer_interceptors.html
You can try to use a
WriterInterceptor
to add this header.Some good examples explained here: https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-edition/content/en/part1/chapter12/reader_and_writer_interceptors.html