Spring Web服务解组无法正常工作 [英] Spring Web-Service unmarshalling not working

查看:121
本文介绍了Spring Web服务解组无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经这样配置了我的WebService:

I have configured my WebService like this:

applicationContext:

applicationContext:

<sws:annotation-driven />    
 <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping" >
<property name="interceptors">
 <list>
    <bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>
</list>
</property>

注意:拦截器是在启动时加载的,但是如果有请求传入,则不会写任何东西.

Note: the Interceptor is loaded on startup, but doesn´t write anything, if a request is coming in.

我有一个类PersonServiceImpl及其方法addPersonRequest().一切正常,如果我使用org.dom4j.Element作为方法参数;

I have a class PersonServiceImpl with the method addPersonRequest(). Everything works , if i am using org.dom4j.Element as method parameter;

@Endpoint
public class PersonServiceImpl {
     @PayloadRoot(namespace = "http://www.example.org/person/schema", localPart = "AddPersonRequest")
     @ResponsePayload
       public AddPersonRequest addPersonRequest(@RequestPayload Element element) {
        System.out.println(element.asXML());
        Person response = new Person();
        response.setId(2);
        response.setFirstName("Mad");
        response.setLastName("Mike");
        return response;
     }
}

但是,如果我更改如下所示的方法参数(因此应使用spring-ws的自动编组),request.getFirstName()将输出null. (JAXB2在类路径上).

But if i change my method parameters like shown below (so auto-marshalling of spring-ws should be used) the request.getFirstName() prints null. (JAXB2 is on classpath).

Person类用@XMLType和@XMLRootElement注释.

The Person-class is annotated with @XMLType and @XMLRootElement.

注意:编组工作正常.

@Endpoint
public class PersonServiceImpl {
     @PayloadRoot(namespace = "http://www.example.org/person/schema", localPart = "AddPersonRequest")
     @ResponsePayload
       public AddPersonRequest addPersonRequest(@RequestPayload Person request, SoapHeader header) {
        System.out.println(header.getName());
        System.out.println(request.getFirstName());
        Person response = new Person();
        response.setId(2);
        response.setFirstName("Mad");
        response.setLastName("Mike");
        return response;
     }
}

Person.java:

Person.java:

@XmlType
@XmlRootElement(namespace="http://www.example.org/person/schema", name="Person")
public class Person implements Serializable {

    private int id;
    private String firstName;
    private String lastName;

    @XmlElement
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @XmlElement
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @XmlAttribute
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
}

通过soapUI(从wsdl生成)发送的测试请求:

Test-Request sent via soapUI (generated from wsdl):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://www.example.org/person/schema"> 
<soapenv:Header/>
<soapenv:Body>

  <sch:AddPersonRequest>

     <sch:Person sch:Id="1">

        <sch:FirstName>firstname</sch:FirstName>

        <sch:LastName>lastname</sch:LastName>

     </sch:Person>

  </sch:AddPersonRequest>

</soapenv:Body>
</soapenv:Envelope>

推荐答案

您提到了编组工作,但我看不到任何理由无法进行编组工作,您是否在信息区中测试了编组和编组?

You mentioned marshalling is working I don't see any reason why unmarshalling won't work, Have you tested marshall and unmarshall in isloation?

只需确保肥皂请求良好,是否可以添加日志记录拦截器并打印来自访问Web服务的客户端的实际请求,将此代码段添加到上下文文件中

Just to make sure the soap request is good, Can you add logging interceptor and print the actual request that is coming from the client accessing web service, Add this snippet to your context file

  <sws:interceptors>
    <bean class="org.springframework.ws.soap.server.endpoint.interceptor.SoapEnvelopeLoggingInterceptor">
        <property name="logRequest" value="true"></property>
        <property name="logResponse" value="true"></property>
    </bean>
</sws:interceptors>

您应该看到这样的日志消息,其中包含整个肥皂请求,我正在将来自saop UI的日志消息请求添加到

You should see a log message like this that contains the entire soap request, I am adding a log message request from a saop UI to

   DEBUG [http-8080-2]:endpoint.interceptor.SoapEnvelopeLoggingInterceptor.logMessage - Request:
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://hoost:port/context/patient/schemas">
      <soapenv:Header/>
         <soapenv:Body>
            <sch:addRequest>
              <sch:patient>
                  <sch:id>?</sch:id>
                     <sch:lastname>Joe</sch:lastname>
                </sch:patient>
             </sch:addRequest>
        </soapenv:Body>
      </soapenv:Envelope>

答案更新

可能不确定您的肥皂要求

It could be your soap request not sure though

        <sch:FirstName>firstname</sch:FirstName> (this should be)

        <sch:firstName>firstname</sch:firstName>

另一个更新

该异常是由于您定义终点的方式所致,在您的肥皂请求( sch:AddPersonRequest )中,您发送的不是addPersonRequest而是Person作为有效负载,因此请更改终点以反映出来, @RequestPayload应该是AddPersonRequest而不是Person

The exception is due to the way you defined the end point, In your soap request (sch:AddPersonRequest) you are sending a addPersonRequest not Person as payload so change the end point to reflect that, The @RequestPayload should be AddPersonRequest not Person

     @PayloadRoot(namespace = "http://www.example.org/person/schema", localPart = "AddPersonRequest")
     @ResponsePayload
      public AddPersonRequest addPersonRequest(@RequestPayload AddPersonRequest request, SoapHeader header) {

这篇关于Spring Web服务解组无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆