Reponse type in Java adapters
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?
我设法创建了一个外部项目,把类放在那里,将它编译成一个jar文件并将它包含在adapters / adapter_name / lib文件夹中(也添加到构建路径中)。
如果您对CorsFilter感兴趣,我建议像下面展示一样实施它,然后将该类添加到您的Application
。
@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);
}
}
}
链接地址: http://www.djcxy.com/p/58276.html
上一篇: java中的类加载器本身就是一个类,那么谁将加载类加载器类?
下一篇: Java适配器中的响应类型