2
votes

I have a class (stored in server/java) that returns a Response type object and I want to use it in adapters.

public class CorsResponse {
public static Response build() {
    return Response
            .ok()
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Methods", "GET, POST")
            .header("Access-Control-Allow-Headers", "accept, origin, content-type")
            .header("Access-Control-Max-Age", "1728000")
            .build();
}

If I use this class in an adapter's method, I get an exception:

javax.servlet.ServletException: java.lang.LinkageError: loader constraint violation: loader (instance of com/ibm/ws/classloading/internal/AppClassLoader) previously initiated loading for a different type with name "javax/ws/rs/core/Response" ...

Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of com/ibm/ws/classloading/internal/AppClassLoader) previously initiated loading for a different type with name "javax/ws/rs/core/Response"

How can I set which Response type to load?

2

2 Answers

1
votes

I have managed to create an external project, put the class there, compile it to a jar file and include it in the adapters/adapter_name/lib folder (added to build path also).

0
votes

If you are interested in a CorsFilter I suggest implementing it like show below and adding the class to your Application instead...

@Provider
public class ResponseCorsFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext requestContext,
            ContainerResponseContext responseContext) throws IOException {

        responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
        responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, OPTIONS");


        String reqHead = requestContext.getHeaderString("Access-Control-Request-Headers");

        if(null != reqHead && !reqHead.equals("")){
            responseContext.getHeaders().add("Access-Control-Allow-Headers", reqHead);
        }

    }


}