[英]Intercept JAX-RS web service response to add JSON field
我有一个JAX-RS Web服务,它返回如下所示的Response对象(它在WebLogic 12.2.1中运行)。 它将向客户端返回JSON响应。 是否可以编写一个拦截器或过滤器,以便在返回Web服务调用时,它将在JSON响应中添加一个额外的字段?
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("LogIn")
public Response logIn(@Context HttpServletRequest request, Parameters requestParameters) {...}
提前致谢。
如果使用Jersey,则可以尝试实现ContainerResponseFilter
。
在Override filter()
,它提供ContainerResponseContext
对象,使您可以访问使用getEntity()
方法发送的响应。
您可以修改该对象并将其设置回响应中。
public class ResponseInterceptor implements ContainerResponseFilter{
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
Object obj = responseContext.getEntity();
// Modify the Response obj as per need
responseContext.setEntity(obj);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.