ContainerResponseFilter not working

In wildfly 8.1 with REST services, I wanted to implement CORS ContainerRequestFilter and ContainerResponseFilter.

My query filter works fine, but the ContainerResponseFilter never loads or gets called

 package org.test.rest; import java.io.IOException; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerResponseContext; import javax.ws.rs.container.ContainerResponseFilter; import javax.ws.rs.container.PreMatching; import javax.ws.rs.ext.Provider; @Provider @PreMatching // <-- EDIT : This was my mistake ! DO NOT ADD THIS public class CorsResponseFilter implements ContainerResponseFilter { public CorsResponseFilter() { System.out.println("CorsResponseFilter.init"); } @Override public void filter(ContainerRequestContext req, ContainerResponseContext resp) throws IOException { System.out.println("CorsResponseFilter.filter"); resp.getHeaders().add("Access-Control-Allow-Origin", "*"); resp.getHeaders().add("Access-Control-Allow-Credentials", "true"); resp.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); resp.getHeaders().add("Access-Control-Allow-Headers", "Content-Type, Accept"); } } 

This seems like a Wildfly / resteasy bug. Do you have another idea / am I missing something?

+6
source share
1 answer

You mix ContainerRequestFilter and ContainerResponseFilter in your question. Since you want to send additional headers to the client, the ContainerResponseFilter is correct.

The @PreMatching annotation can be applied to the ContainerRequestFilter "to indicate that such a filter should be applied globally to all resources in the application before the actual resource matching occurs."

Adding it to a ContainerResponseFilter does not make sense. Just remove the annotation and your filter should work.

+5
source

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


All Articles