本文介绍了在ContainerResponseFilter的获取响应(JAX-RS 2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我尝试移除此CORS过滤器到JAX-RS 2。I am trying to port this CORS filter to JAX-RS 2.但是,我看不到如何获取 Response 对象(如旧代码中的)从 ContainerResponseContext 我得到传递的覆盖方法 ContainerResponseFilter 。However, I do not see how to get the Response object (as in the old code) from the ContainerResponseContext I get passed in the overridden method of ContainerResponseFilter.如果有一个更优雅的方式来做CORS与JAX-RS 2,这当然是首选。提前感谢。If there is a more elegant way to do CORS with JAX-RS 2, that would be preferrable of course. Thanks in advance.推荐答案可直接访问 ContainerResponseContext :@Providerpublic class ResponseCorsFilter implements ContainerResponseFilter{ @Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { responseContext.getHeaders() .putSingle("Access-Control-Allow-Origin","*"); responseContext.getHeaders() .putSingle("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); List<String> reqHead=requestContext.getHeaders() .get("Access-Control-Request-Headers"); if(null != reqHead){ responseContext.getHeaders() .put("Access-Control-Allow-Headers", new ArrayList<Object>(reqHead)); } }} 这篇关于在ContainerResponseFilter的获取响应(JAX-RS 2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-26 04:10