Getting response in ContainerResponseFilter (JAX-RS 2)

I am trying to pass this CORS filter to JAX-RS 2.

However, I don’t see how to get the Response object (as in the old code) from the ContainerResponseContext I get the one passed in the overridden method ContainerResponseFilter .

If there is a more elegant way to do CORS with JAX-RS 2, that would be preferable, of course. Thanks in advance.

+4
source share
1 answer

Thre's answer is directly available as a ContainerResponseContext :

 @Provider public 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)); } } } 
+10
source

Source: https://habr.com/ru/post/1493651/


All Articles