同事,我称之为Web服务并收到异常:
org.springframework.ws.soap.SoapMessageCreationException:无法从InputStream创建消息:无效的内容类型:text / html。这是一条错误消息而不是SOAP响应吗?嵌套异常是com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:无效的内容类型:text / html。这是一条错误消息而不是SOAP响应吗?
Wireshark显示比我收到回复:
HTTP/1.1 200 OK
Content-Type: text/html
Transfer-Encoding: chunked
Server: Jetty(8.1.13.v20130916)
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://mayacomp/Generic/Ws">
<soapenv:Body>
<ws:response>
<out>
<requestID>6b140165-af79-47a2-9e9f-5b7bba265050</requestID>
<requestTimestamp>2015-12-01T13:04:44.044</requestTimestamp>
</out>
</ws:response>
</soapenv:Body>
</soapenv:Envelope>
我的豆子看起来像:
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.mayacomp.entities");
HashMap<String,Object> properties = new HashMap<String,Object>();
properties.put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setMarshallerProperties(properties);
return marshaller;
}
@Bean
public myClient myClient(Jaxb2Marshaller marshaller) {
myClient client = new myClient();
client.setDefaultUri("URL");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
//client.setMessageSender(messageSender);
return client;
}
调用WS的类看起来像:
public class myClient extends WebServiceGatewaySupport {
public Response createApplication(In in) {
Request request = (Request) new Request();
request.setIn(in);
Response response = (Response) getWebServiceTemplate().marshalSendAndReceive(
"URL",
request,
new WebServiceMessageCallback()
{ public void doWithMessage(WebServiceMessage message) {
SaajSoapMessage saajSoapMessage = (SaajSoapMessage)message;
SOAPMessage soapMessage = SOAP.createSOAPMessage(in);
saajSoapMessage.setSaajMessage(soapMessage);
}
}
);
return null;
}
你能帮我看看网络服务回复吗?
UPDATE
--- maven-dependency-plugin:2.8:tree (default-cli) @ adapter ---
com.comp:adapter:jar:0.0.1-SNAPSHOT
+- junit:junit:jar:4.12:compile
| \- org.hamcrest:hamcrest-core:jar:1.3:compile
+- org.springframework:spring-context:jar:4.2.3.RELEASE:compile
| +- org.springframework:spring-aop:jar:4.2.3.RELEASE:compile
| | \- aopalliance:aopalliance:jar:1.0:compile
| +- org.springframework:spring-beans:jar:4.2.3.RELEASE:compile
| \- org.springframework:spring-expression:jar:4.2.3.RELEASE:compile
+- org.springframework:spring-core:jar:4.2.3.RELEASE:compile
| \- commons-logging:commons-logging:jar:1.2:compile
+- io.codearte.jfairy:jfairy:jar:0.5.1:compile
| +- com.google.guava:guava:jar:19.0-rc2:compile (version selected from constraint [15.0,))
| +- org.yaml:snakeyaml:jar:1.16:compile (version selected from constraint [1.9,2.0))
| +- org.iban4j:iban4j:jar:2.1.1:compile
| +- com.google.inject:guice:jar:4.0:compile
| | \- javax.inject:javax.inject:jar:1:compile
| +- joda-time:joda-time:jar:2.3:compile
| +- com.google.inject.extensions:guice-assistedinject:jar:4.0:compile
| +- org.reflections:reflections:jar:0.9.9:compile
| | +- org.javassist:javassist:jar:3.18.2-GA:compile
| | \- com.google.code.findbugs:annotations:jar:2.0.1:compile
| +- org.apache.commons:commons-lang3:jar:3.3.2:compile
| \- org.slf4j:slf4j-api:jar:1.7.7:compile
+- org.fluttercode.datafactory:datafactory:jar:0.8:compile
+- org.springframework.ws:spring-ws-core:jar:2.2.3.RELEASE:compile
| +- org.springframework.ws:spring-xml:jar:2.2.3.RELEASE:compile
| +- org.springframework:spring-oxm:jar:4.0.9.RELEASE:compile
| +- org.springframework:spring-web:jar:4.0.9.RELEASE:compile
| \- org.springframework:spring-webmvc:jar:4.0.9.RELEASE:compile
+- log4j:log4j:jar:1.2.17:compile
\- org.apache.httpcomponents:httpclient:jar:4.5.1:compile
+- org.apache.httpcomponents:httpcore:jar:4.4.3:compile
\- commons-codec:commons-codec:jar:1.9:compile
查看堆栈跟踪,其中抛出org.springframework.ws.soap.SoapMessageCreationException。根据错误发生的位置,您可以配置org.springframework.ws.client.core.WebServiceTemplate以接受Content-Type:text / html。
我有一种感觉,即实现的这个方面是不可插拔的,你将无法替换它。见SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?。某些jdk代码中出现了基本异常。
如果是我,我很想使用常规的http客户端,进行http调用,并手动解析响应。这里有一些使用apache客户端执行此操作的完整示例:Sending HTTP Post request with SOAP action using org.apache.http
我的一个供应商服务面临同样的问题,它将Content-Type作为text / html返回,而spring web服务模板不喜欢这样。因为没有简单的方法来更改spring web-service中的响应头。我通过使用自定义Http客户端解决了它,然后提供了删除“Content-Type”标头的客户端拦截器。
HttpClient httpClient = HttpClients.custom()
.addInterceptorFirst(new HttpComponentsMessageSender.RemoveSoapHeadersInterceptor())
.addInterceptorFirst(new HttpResponseInterceptor() {
@Override
public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
boolean htmlResponse = Arrays.stream(httpResponse.getAllHeaders()).anyMatch(header -> header.getName().equalsIgnoreCase("Content-Type") && header.getValue().contains("text/html"));
if (htmlResponse) {
httpResponse.removeHeaders("Content-Type");
}
}
})
.setDefaultRequestConfig(requestConfig)
.build();
我刚删除并没有添加“Content-Type”标头,因为SOAP 1.1和1.2的SOAPMessageFactory在处理消息之前添加了适当的标头。