【发布时间】:2018-06-22 20:42:44
【问题描述】:
我正在使用 Apache CXF 实现 SOAP Web 服务。我正在使用 Jboss EAP 服务器。我已使用以下代码公开 SOAP Web 服务。
CxfComponent cxfComponent = new CxfComponent(context);
CxfEndpoint serviceEndpoint = new CxfEndpoint(FPSoapServiceConstants.WSDL_CONFIG_URI, cxfComponent);
serviceEndpoint.setDataFormat(DataFormat.PAYLOAD);
serviceEndpoint.setServiceClass(com.fp.en.webservices.fulfillment.FulfillmentService.class);
HashMap<String, Object> properties = new HashMap<>();
properties.put("faultStackTraceEnabled", true);
properties.put("exceptionMessageCauseEnabled", true);
serviceEndpoint.configureProperties(properties);
serviceEndpoint.setLoggingFeatureEnabled(true);
context.addEndpoint(FPSoapServiceConstants.SOAP_ENDPOINT_FULFILLMENT_SERVICE, serviceEndpoint);
我正在使用 apache camel 来处理传入的肥皂消息
route.process(fpSOAPRequestProcessor).process(xyzProcessor).process(fpSOAPResponseProcessor)
我想获取对象中的所有参数我创建了一个类并尝试获取正文
BuyProductRequest buyRequest = message.getBody(BuyProductRequest.class);
但这给了我null。但是当我试图得到 p>
String buyRequest = message.getBody(String.class);
它给了我 SOAP 消息所以我必须通过 JAXB Marshaller 将 xml SOAP 消息转换为 Object。 处理器代码如下
public class FPSoapRequestProcessor implements Processor{
@Override
public void process(Exchange exchange) throws Exception {
Message message = exchange.getIn();
String operation = String.valueOf(exchange.getIn().getHeader("operationName"));
if(FPSoapServiceConstants.BUY_PRODUCT_SOAP_OPERATION.equalsIgnoreCase(operation)) {
populateBuyProductOperationProperties(message);
}
}
private void populateBuyProductOperationProperties(Message message) {
String buyRequest = message.getBody(String.class);
BuyProductRequest productInfo= parseRequest(buyRequest);
message.setHeader("MSISDN", productInfo.getMsisdn());
message.setHeader("iname", productInfo.getIname());
message.setHeader("input", productInfo.getInput());
message.setHeader("username", productInfo.getUserName());
message.setHeader("password", productInfo.getPassword());
message.setHeader("soapConversion", true);
}
private BuyProductRequest parseRequest(String soapRequest){
try(InputStream is = new ByteArrayInputStream(soapRequest.getBytes())) {
JAXBContext jaxbContext = JAXBContext.newInstance(BuyProductRequest.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (BuyProductRequest) jaxbUnmarshaller.unmarshal(is);
} catch (Exception e) {
throw new RuntimeException("SOAP Request Object Resolving Error",e);
}
}
}
那么有什么简单的方法来构造请求对象吗?类似地,当我完成处理时,在 fpSOAPResponseProcessor 中我必须将我的对象转换为肥皂字符串然后我发送它。
fpSoapResponseProcessor 代码如下
public class FDPSoapResponseProcessor implements Processor{
@Override
public void process(Exchange exchange) throws Exception {
Message message = exchange.getIn();
FulfillmentResponse response = XmlUtil.unmarshall(message.getBody(String.class), FulfillmentResponse.class);
BuyProductResponse buyProductResponse = new BuyProductResponse();
buyProductResponse.setProductResponse(response);
String soapResponse = parse(buyProductResponse);
exchange.getOut().setBody(soapResponse);
}
private String parse(BuyProductResponse buyProductResponse) {
try(StringWriter writer = new StringWriter()){
JAXBContext jContext = JAXBContext.newInstance(BuyProductResponse.class);
Marshaller marshallObj = jContext.createMarshaller();
marshallObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshallObj.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
marshallObj.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshallObj.setProperty("com.sun.xml.bind.namespacePrefixMapper", new FulfillmentResponseMapper());
marshallObj.marshal(buyProductResponse, writer);
return writer.toString();
} catch(Exception e) {
throw new RuntimeException("SOAP Request String Parsing Error",e);
}
}
private static class FulfillmentResponseMapper extends NamespacePrefixMapper {
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
if(FPSoapServiceConstants.SOAP_NAMESPACE_URI.equalsIgnoreCase(namespaceUri)) {
return FPSoapServiceConstants.SOAP_PREFIX;
}
return suggestion;
}
@Override
public String[] getPreDeclaredNamespaceUris() {
return new String[] { FPSoapServiceConstants.SOAP_NAMESPACE_URI};
}
}
}
如果有,请推荐一个合适的简单方法?
【问题讨论】:
标签: java web-services soap apache-camel cxf