用弹簧解组SOAP响应

时间:2016-06-04 21:38:21

标签: java spring web-services jaxb unmarshalling

我试图用SpringWS调用一个肥皂ws: 在下面你可以找到:

-applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory" />

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="com.al.factories" />
    </bean>

    <bean id="WebServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
        <constructor-arg ref="messageFactory" />
        <property name="marshaller" ref="marshaller"></property>
        <property name="unmarshaller" ref="marshaller"></property>
        <property name="messageSender">
            <bean
                class="org.springframework.ws.transport.http.HttpComponentsMessageSender" />
        </property>
        <property name="defaultUri"
            value="http://localhost:8080/web_services/hello" />
    </bean>

    <bean id="yourServiceClient" class="com.al.client.YourServiceClient">
        <property name="webServiceTemplate" ref="WebServiceTemplate" />
    </bean>


</beans>

-ObjectFactory.java

package com.al.factories;

import javax.xml.bind.annotation.XmlRegistry;

import com.al.wsmapping.InputParameters;
import com.al.wsmapping.Job;

@XmlRegistry
public class ObjectFactory {

    public ObjectFactory() {
    }

    public InputParameters createYourRequest() {
        return new InputParameters();
    }

    public Job createYourResponse() {
        return new Job();
    }

}

-InputParameters.java

package com.al.wsmapping;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
//@XmlType(name = "", propOrder = { "paramONE" })
@XmlRootElement(name = "getJoboolone", namespace = "http://ws.mkyong.com/")
public class InputParameters {
}

-YourServiceClient.java

package com.al.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ws.client.core.WebServiceTemplate;

import com.al.factories.ObjectFactory;
import com.al.wsmapping.InputParameters;
import com.al.wsmapping.Job;

@Component("yourServiceClient")
public class YourServiceClient implements YourService {

    private static final ObjectFactory WS_CLIENT_FACTORY = new ObjectFactory();

    @Autowired
    private WebServiceTemplate webServiceTemplate;

    public String getValue() {
        InputParameters request = WS_CLIENT_FACTORY
                .createYourRequest();

        Job response = (Job) webServiceTemplate.marshalSendAndReceive(request);

        System.out.println(response.getJobName());

        return response.toString();
    }

我定义了一个简单的客户端来测试它,使用main方法,用SpringWS调用它,它可以工作,但我可以解组响应。我知道我应该用xml映射做一些事情,但我不知道它是怎么做的。 我的WS方法没有参数。 如果有人能给我一些建议,那将是有价值的! 我发现了很多例子,但没有人适合我的需要...... 下面你可以看到我对SOAPUI的请求和响应,正如你所看到的,后端服务按预期工作。

-SOAPUI REQUEST

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.mkyong.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:getJoboolone/>
   </soapenv:Body>
</soapenv:Envelope>

-SOAPUI回应

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:getJobooloneResponse xmlns:ns2="http://ws.mkyong.com/">
         <return>
            <jobId>30</jobId>
            <jobName>Dev presso Accenture</jobName>
            <jobRole>Developer</jobRole>
            <salary>3000</salary>
            <type>Omnesciente</type>
            <userId>
               <password>ererer</password>
               <userId>1</userId>
               <userName>ivoru84</userName>
            </userId>
         </return>
      </ns2:getJobooloneResponse>
   </S:Body>
</S:Envelope>

-STACKTRACE

giu 05, 2016 2:31:43 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFORMAZIONI: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@238e0d81: startup date [Sun Jun 05 14:31:43 BST 2016]; root of context hierarchy
giu 05, 2016 2:31:43 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFORMAZIONI: Loading XML bean definitions from class path resource [applicationContext.xml]
giu 05, 2016 2:31:43 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFORMAZIONI: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1554909b: defining beans [messageFactory,marshaller,WebServiceTemplate,yourServiceClient]; root of factory hierarchy
giu 05, 2016 2:31:43 PM org.springframework.ws.soap.saaj.SaajSoapMessageFactory afterPropertiesSet
INFORMAZIONI: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
giu 05, 2016 2:31:43 PM org.springframework.oxm.jaxb.Jaxb2Marshaller createJaxbContextFromContextPath
INFORMAZIONI: Creating JAXBContext with context path [com.al.factories]
Exception in thread "main" org.springframework.ws.client.WebServiceIOException: I/O error: Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out
    at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:543)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:384)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:378)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:370)
    at com.al.client.YourServiceClient.getValue(YourServiceClient.java:23)
    at com.al.maintest.MainTest.main(MainTest.java:16)
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:170)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:166)
    at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:90)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:281)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:92)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)
    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)
    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)
    at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:219)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)
    at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:715)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:520)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
    at org.springframework.ws.transport.http.HttpComponentsConnection.onSendAfterWrite(HttpComponentsConnection.java:119)
    at org.springframework.ws.transport.AbstractWebServiceConnection.send(AbstractWebServiceConnection.java:47)
    at org.springframework.ws.client.core.WebServiceTemplate.sendRequest(WebServiceTemplate.java:622)
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:585)
    at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:537)
    ... 5 more

提前谢谢! A.D。

0 个答案:

没有答案