Java 发送前修改springframework.ws.WebServiceMessageXML
通过SOAP UI测试的有效SOAP请求:Java 发送前修改springframework.ws.WebServiceMessageXML,java,xml,spring,web-services,soap,Java,Xml,Spring,Web Services,Soap,通过SOAP UI测试的有效SOAP请求: <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="some_ns1_uri">
<SOAP-ENV:Header />
<ns0:Body>
<ns1:AAA_method>
<ns1:A>A</ns1:A>
<ns1:B>B</ns1:B>
<ns1:C>
<ns1:string>C_string</ns1:string>
</ns1:C>
<ns1:D>
<ns1:string>D_string</ns1:string>
</ns1:D>
<ns1:E>
<ns1:val>E_val</ns1:val>
</ns1:E>
<ns1:F>
<ns1:val>F_val</ns1:val>
</ns1:F>
</ns1:AAA_method>
</ns0:Body>
</SOAP-ENV:Envelope>
WebServiceTemplate
配置如下所示:
@Configuration
public class ClientConfig {
@Value("${ws.username}") private String username;
@Value("${ws.password}") private String usernamePassword;
@Value("${ws.uri}") private String endpointUri;
@Value("${ws.contextPath}") private String cxtPath;
@Bean
Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath(cxtPath); // package location where wsdl generated classes are
return marshaller;
}
@Bean
HttpClient httpClient() {
List<Header> headers = new ArrayList<>();
String userAndPassword = username + ":" + usernamePassword;
BasicHeader authHeader = new BasicHeader("Authorization", "Basic " + new String(Base64.encode(userAndPassword.getBytes())));
headers.add(authHeader);
RequestDefaultHeaders reqHeader = new RequestDefaultHeaders(headers);
return HttpClients.custom()
.addInterceptorFirst(new HttpComponentsMessageSender.RemoveSoapHeadersInterceptor())
.addInterceptorLast(reqHeader).build();
}
@Bean
HttpComponentsMessageSender defaultMyMessageSender() {
return new HttpComponentsMessageSender(httpClient());
}
@Bean
public WebServiceTemplate webServiceTemplate() {
WebServiceTemplate wsTemplate = new WebServiceTemplate();
wsTemplate.setDefaultUri(endpointUri);
wsTemplate.setMarshaller(marshaller());
wsTemplate.setMessageSender(defaultMyMessageSender());
return wsTemplate;
}
}
<s:complexType name="e_value">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="val" type="s:dateTime" />
</s:sequence>
</s:complexType>
对于缺少的信封名称空间,我已将以下内容添加到doWithMessage
:
...
SoapEnvelope envelope = soapMessage.getEnvelope();
envelope.addNamespaceDeclaration(nXsi, nXsiUri);
envelope.addNamespaceDeclaration(nNs0, nNs0Uri);
envelope.addNamespaceDeclaration(nNs1, nNs1Uri);
/*
Results:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="some_ns1_uri"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
*/
...
E
和F
值是xmlgoriiancalendar
类型日期,我使用的是2021-01-05
格式日期
从wsdl文件中,我看到e_值
指定如下:
@Configuration
public class ClientConfig {
@Value("${ws.username}") private String username;
@Value("${ws.password}") private String usernamePassword;
@Value("${ws.uri}") private String endpointUri;
@Value("${ws.contextPath}") private String cxtPath;
@Bean
Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath(cxtPath); // package location where wsdl generated classes are
return marshaller;
}
@Bean
HttpClient httpClient() {
List<Header> headers = new ArrayList<>();
String userAndPassword = username + ":" + usernamePassword;
BasicHeader authHeader = new BasicHeader("Authorization", "Basic " + new String(Base64.encode(userAndPassword.getBytes())));
headers.add(authHeader);
RequestDefaultHeaders reqHeader = new RequestDefaultHeaders(headers);
return HttpClients.custom()
.addInterceptorFirst(new HttpComponentsMessageSender.RemoveSoapHeadersInterceptor())
.addInterceptorLast(reqHeader).build();
}
@Bean
HttpComponentsMessageSender defaultMyMessageSender() {
return new HttpComponentsMessageSender(httpClient());
}
@Bean
public WebServiceTemplate webServiceTemplate() {
WebServiceTemplate wsTemplate = new WebServiceTemplate();
wsTemplate.setDefaultUri(endpointUri);
wsTemplate.setMarshaller(marshaller());
wsTemplate.setMessageSender(defaultMyMessageSender());
return wsTemplate;
}
}
<s:complexType name="e_value">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="val" type="s:dateTime" />
</s:sequence>
</s:complexType>
所以我猜默认的
dateTime
formatter调用了这个错误?标记名应该不重要,也不应该在哪里,只要声明在那里。@M.Deinum你确定还是仅仅“应该不重要”?因为我无法发送请求。你能分享任何信息来源吗?理论上,这并不重要,除非接收端的编程方式符合预期。但从XML的角度来看,这并不重要。发送时是否出现错误?如果是这样,请将此添加到您的问题中。@M.Deinum addedUpdate#1
@M.Deinum固定日期格式化程序问题它有2021-01-04T00:00:00
format。。现在需要设置解组器,应该可以了。我将很快添加更新#2
<s:complexType name="e_value">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="val" type="s:dateTime" />
</s:sequence>
</s:complexType>