如何使用Spring-ws客户端读取具有无效内容类型的Web服务的响应?

时间:2015-12-01 12:43:21

标签: web-services soap spring-ws

Collegues,我称之为webservice并收到异常:

  

org.springframework.ws.soap.SoapMessageCreationException:无法   从InputStream创建消息:无效的内容类型:text / html。是   这是一条错误消息而不是SOAP响应?嵌套异常是   com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:无效   内容类型:text / html的。这是一条错误消息而不是SOAP   响应?

我看了thisthis

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;
    }

你能帮助我阅读网络服务回复吗?

更新

--- 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

3 个答案:

答案 0 :(得分:1)

查看堆栈跟踪,其中抛出 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

答案 1 :(得分:1)

我的供应商服务之一遇到相同的问题,该服务返回Content-Type作为text / html,而Spring Web服务模板不喜欢这样。 由于在Spring Web服务中没有简单的方法来更改响应标头。我通过使用自定义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在处理消息之前添加了适当的标头。

答案 2 :(得分:0)

我遇到了和你一样的 Content-Type:text/html 问题 所以我所有的注意力都集中在尝试将 CT 更改为 text/xml 但这不起作用。问题是您的“MyClient”需要有正确的“MessageSender”和我在 pom 中添加的依赖项。

 @Bean
public HttpComponentsMessageSender defaultMessageSender() {
    HttpComponentsMessageSender messageSender = createMessageSenderBasicAuth();

    HttpClientBuilder httpClientBuilder = HttpClients.custom()
            .useSystemProperties()
            .addInterceptorFirst(new HttpComponentsMessageSender.RemoveSoapHeadersInterceptor())
            .setDefaultCredentialsProvider(basicAuthCredentials())
            .setDefaultRequestConfig(requestConfigWithDefaultTimeout());

    messageSender.setHttpClient(httpClientBuilder.build());

    return messageSender;
}
private HttpComponentsMessageSender createMessageSenderBasicAuth() {
    return new HttpComponentsMessageSender() {
        @Override
        protected HttpContext createContext(URI uri) {
            HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
            AuthCache authCache = new BasicAuthCache();
            BasicScheme basicAuth = new BasicScheme();
            authCache.put(target, basicAuth);
            HttpClientContext localContext = HttpClientContext.create();
            localContext.setAuthCache(authCache);

            return localContext;
        }
    };
}

private RequestConfig requestConfigWithDefaultTimeout() {
    return RequestConfig.custom()
            .setConnectionRequestTimeout(10000)
            .setConnectTimeout(10000) // 10 sec
            .setSocketTimeout(10000)
            .build();
}

在我添加的 pom 文件中

pom.xml

    <dependency>
        <groupId>org.springframework.ws</groupId>
        <artifactId>spring-ws-core</artifactId>
        <version>2.4.7.RELEASE</version>
    </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.12.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.2</version>
        </dependency>

你的“MyClient”应该是这样的

@Bean
public myClient myClient(@Qualifier("marshaller") Jaxb2Marshaller marshaller) {
    myClient client = new myClient();
    client.setDefaultUri("URL");
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);
    client.setMessageSender(defaultMessageSender());
    return client;
}

打电话是个好主意

WebServiceTemplate webServiceTemplate = getWebServiceTemplate(); webServiceTemplate.afterPropertiesSet(); 在您的“公共类 myClient 扩展 WebServiceGatewaySupport”类中