用于xsd:ComplexType对象的RestController中@RequestBody中的空值

时间:2018-12-11 15:35:53

标签: java spring rest spring-mvc

下面是我的代码:

@RequestMapping(value = "/salvageTest" , method = RequestMethod.POST,consumes= MediaType.APPLICATION_XML, produces = "application/xml")
public @ResponseBody SalvageUpdateTestResponse restTestfunction(@RequestBody SalvageUpdateTestRequest request ) {

    String vehCollDate = request.getSalvageUpdateTestBody().getVehCollectedDate();//returns null
    String salMes = request.getSalvageUpdateTestBody().getSalvageMessage();//null

    System.out.println(vehCollDate);
    System.out.println(salMes);

    SalvageUpdateTestResponse response = new SalvageUpdateTestResponse();
    response.setSalvageUpdateTestResponseBody(request.getSalvageUpdateTestHeader()); //returns non null since header is type string
    return response;

}

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services.xsd
    http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
    http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<context:component-scan base-package="com.package.*" />
<mvc:annotation-driven />

XSD:     

<xs:element name="salvageUpdateTestRequest" type="tns:salvageUpdateTestRequest"/>

    <xs:complexType name="salvageUpdateTestRequest">
        <xs:sequence>
            <xs:element name="salvageUpdateTestHeader"
                type="xs:string" />
            <xs:element name="salvageUpdateTestBody"
                type="tns:salvageUpdateTestBody" />
        </xs:sequence>
    </xs:complexType>


<xs:element name="salvageUpdateTestResponse">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="salvageUpdateTestResponseBody"
                type="xs:string" />
        </xs:sequence>
    </xs:complexType>
</xs:element>


<xs:complexType name="salvageUpdateTestBody">
    <xs:sequence>
        <xs:element name="VehCollectedDate" type="xs:string"
            minOccurs="0" />
        <xs:element name="SalvageMessage" type="xs:string"
            minOccurs="0" />
    </xs:sequence>
</xs:complexType>

我正在使用JAXB2Marshaller解组xml @RequestBody。完全解组简单的String值(非复杂类型)。但是Iam为复杂类型对象获取了null值。 我想我可能会在spring-servlet.xml中丢失一些配置。

这真的延迟了我的项目进度

请帮助。

更新:我在spring-servlet.xml中添加了以下代码,现在可以正确解组。 但是我想创建另一个类似的@RequestMapping端点,并且需要配置相同的端点。 我尝试添加单独的内容:test.salvage.update.generation,但是会引发错误。

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean id="marshallingHttpMessageConverter"
              class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
            <property name="marshaller" ref="jaxb2Marshaller"/>
            <property name="unmarshaller" ref="jaxb2Marshaller"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">    

    <property name="packagesToScan">
      <list>
        <value>test.salvage.update.generated</value>
      </list>
    </property>
</bean>

0 个答案:

没有答案