gpt4 book ai didi

java - 两个不同 soap 版本的系统属性 "javax.xml.soap.MessageFactory"

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:22:15 25 4
gpt4 key购买 nike

我需要从我的应用程序中与两个网络服务通信。对于一个网络服务,我需要使用 soap1_1 版本,而对于另一个 soap 版本,我需要使用 soap1_2。在这种情况下,应该为系统属性“javax.xml.soap.MessageFactory”设置什么值

客户 1:

public class SoapClient1 {


protected static Logger _logger = Logger.getLogger ("TEST");
private static Long retryDelay = null;


public String sendSoapMessage (String xml) throws Exception {

SOAPMessage resp = null;
String response = null;
String endpoint = "http:xxxx";



System.setProperty("javax.xml.soap.MessageFactory","com.sun.xml.internal.messaging.saaj.soap.ver1_2.SOAPMessageFactory1_2Impl");
SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = connectionFactory.createConnection();

long start = System.currentTimeMillis();
long end = System.currentTimeMillis();

//URL endPoint = new URL(endpoint);

//setting connection time out and read timeout

URL endPoint = new URL (null, endpoint, new URLStreamHandler () {
@Override
protected URLConnection openConnection (URL url) throws IOException {

URL clone = new URL (url.toString ());
URLConnection connection = clone.openConnection ();
connection.setConnectTimeout (60000);
connection.setReadTimeout (60000);
// Custom header

return connection;
}});


try{

start = System.currentTimeMillis();


resp = soapConnection.call(getSoapRequest(xml), endPoint);

end = System.currentTimeMillis();

ByteArrayOutputStream os = new ByteArrayOutputStream();
resp.writeTo(os);
response = os.toString();


if (!resp.getSOAPBody().hasFault()) {

response = "SucCess:" + response;

}else{

response = "FaiLure:" + response;
}

}else{

response = "FaiLure:" + response;
}


}catch(SOAPException se){
_logger.log(Level.ERROR," Service Provisioning Call Failed");
_logger.log(Level.ERROR,"The call duration before SOAPException =" +(end-start)+" ms.");

se.printStackTrace();
throw se;
}

soapConnection.close();
return response;
}


private SOAPMessage getSoapRequest(String xml) throws SOAPException,Exception{

MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
/* Create a SOAP message object. */
SOAPMessage soapMessage = mf.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPBody soapBody = soapEnvelope.getBody();
soapEnvelope.getHeader().detachNode();
soapEnvelope.addNamespaceDeclaration("soap","http://yyyy");
SOAPHeader header = soapEnvelope.addHeader();



DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);

InputStream stream = new ByteArrayInputStream(xml.getBytes());
Document doc = builderFactory.newDocumentBuilder().parse(stream);

_logger.log(Level.DEBUG, "Adding SOAP Request Body");
soapBody.addDocument(doc);

soapMessage.saveChanges();

return soapMessage;

}
}

sample 请求

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:soap="http://bridgewatersystems.com/xpc/tsc/entity/soap">
<env:Header/>
<env:Body>
<TempTierChangeRequest xmlns="http://bridgewatersystems.com/xpc/tsc/entity/soap" credentials="root" principal="root">
<temp-tier-change xmlns="">
<service-components>
<service-component name="DSL_Tier_2"/>
</service-components>
<duration-sec>300</duration-sec>
<description>1024 SC</description>
<activation-date>2017-02-09T10:29:16</activation-date>
<subscriber-id>26752018010@wholesale1.com</subscriber-id>
<partition-key>26752018010</partition-key>
<ttc-id>3706043</ttc-id>
<validity-period>
<duration>30</duration>
<expires-with-billing-reset>1</expires-with-billing-reset>
</validity-period>
</temp-tier-change>
</TempTierChangeRequest>
</env:Body>
</env:Envelope>

最佳答案

不可能为两个不同的目的设置系统变量 javax.xml.soap.MessageFactory 的值。默认值是为 SOAP 1.1 设置的

删除系统属性 javax.xml.soap.MessageFactory 并根据您正在构建的客户端类型使用

使用 MessageFactory.newInstance() 构建 soap 消息

如果要SOAP1.1,使用默认构造函数

 MessageFactory factory = MessageFactory.newInstance();

如果你想要SOAP1.2,使用

MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);

参见 Java Tutorial .

JAX-WS 客户端配置了注解@BindingType

@BindingType 在使用注释配置 JAX-WS 客户端时使用,例如,如果客户端是从 WSDL 生成的。将注释添加到端口以将绑定(bind)设置为 SoapBinding.SOAP11HTTP_BINDINGSoapBinding.SOAP12HTTP_BINDING

@WebService(targetNamespace = "https://myservice.services.com", name = "myserviceProxyProt")
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public interface MyServiceProxyPort {

关于java - 两个不同 soap 版本的系统属性 "javax.xml.soap.MessageFactory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41846856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com