要解决"Aache-CXF WS客户端与Eclipse MOXY JAXBContextFactory不发送安全头"的问题,您可以尝试以下解决方法。
import org.eclipse.persistence.jaxb.JAXBContextFactory;
public class CustomJAXBContextFactory extends JAXBContextFactory {
public static JAXBContext createJAXBContext(String contextPath, ClassLoader classLoader, Map properties) throws JAXBException {
// 调用父类方法创建JAXBContext
JAXBContext context = super.createJAXBContext(contextPath, classLoader, properties);
// 添加发送安全头的代码
Client client = ClientProxy.getClient(context);
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy policy = conduit.getClient();
policy.setAllowChunking(false); // 禁用分块传输
// 设置其他安全相关的参数
return context;
}
}
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
public class MyClient {
public static void main(String[] args) {
// 创建JAXRSClientFactoryBean
JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
bean.setAddress("http://localhost:8080/api"); // 设置服务端地址
bean.setResourceClass(MyService.class); // 设置服务类
// 设置CustomJAXBContextFactory
Map properties = new HashMap<>();
properties.put(JAXBContextFactory.CUSTOM_CONTEXT_FACTORY, new CustomJAXBContextFactory());
bean.setProperties(properties);
// 创建WebClient
WebClient client = bean.createWebClient();
// 发送请求
Response response = client.get();
System.out.println(response.readEntity(String.class));
// 关闭客户端
client.close();
}
}
这样,您的Aache-CXF WS客户端就会使用CustomJAXBContextFactory发送安全头了。请根据您的需求添加适当的安全头参数。