在Spring MVC REST中映射到对象

时间:2015-03-12 11:01:40

标签: xml spring rest spring-mvc

所以我对Spring MVC相当新,并且一直在构建一个托管+使用多个API的应用程序,其中大部分工作正常,但我现在碰到了一堵墙。

由于某种原因,xml中的属性不会映射到对象中。我还没有遇到过JSON这个问题,但不幸的是在这种情况下,我在接收端我无法改变请求的格式。 请求有效负载类似于:

<xml>
 <ToUserName> <![CDATA[toUser]]> </ToUserName>
 <FromUserName> <![CDATA[fromUser]]> </FromUserName> 
 <CreateTime> 1348831860 </CreateTime>
 <MsgType> <![CDATA[text]]> </MsgType>
 <Content><![CDATA[this is a test]]></Content>
 <MsgId>1234567890123456</MsgId>
</xml>

我试图将它映射到具有这些+更多属性的类,但是那些在不需要时可以保持为null。 现在我的控制器方法是:

@RequestMapping(value = "/msg", method = RequestMethod.POST, consumes = "application/xml", produces = "application/xml")
    @ResponseBody
    public void receive(@RequestBody ReceivedMessage msg) {
        //TODO: do something with the message here... for now just dump into log
        System.out.println(msg);
    }

好像应该有用,对吗? 好吧,输出就像

ReceivedMessage{toUserName=null, fromUserName=null, createTime=null, msgType=null, msgId=null, medes=null, content=null, picUrl=null, format=null, thumbMediaId=null, location_X=null, location_Y=null, label=null, scale=null, title=null, description=null, url=null, recognition=null, event=null, eventKey=null, ticket=null, latitude=null, longitude=null, precision=null}

如您所见,所有属性都为null,即使是有效负载中提供的属性......

我认为它可能与有效载荷的结构方式有关,但正如我所说,我无法改变它。

非常感谢任何帮助,谢谢。

编辑:修正了标题中的拼写错误... EDIT2:进一步的信息... servlet配置的相关部分:

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
    <bean id="xmlConverter" class="org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter"/>
    <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonConverter" />
                <ref bean="xmlConverter" />
            </list>
        </property>
    </bean> 

EDIT3:ReceivedMessage(省略了getter和setter):

public class ReceivedMessage{
    //common
    private String toUserName;
    private String fromUserName;
    private Long createTime;
    private String msgType;

    private Long msgId;

    private String medes;

    //text
    private String content;
    //image
    private String picUrl;
    //voice + speech recognition
    private String format;
    //video
    private String thumbMediaId;
    //location
    private Double location_X;
    private Double location_Y;
    private String label;
    private Double scale;
    //link
    private String title;
    private String description;
    private String url;
    //speech recognition
    private String recognition;
    //event + QR code scan + location incident + menu event
    private String event;
    //QR code scan + menu event
    private String eventKey;
    //QR code scan
    private String ticket;
    //location incident
    private Double latitude;
    private Double longitude;
    private Double precision;

    public ReceivedMessage() {
    }
}

1 个答案:

答案 0 :(得分:2)

Jaxb2RootElementHttpMessageConverter用于将Java对象转换为XML或从XML转换。如果消息转换满足以下条件,则消息转换有效:

  • 如果类路径中存在JAXB2,则此转换器已预先启用。
  • java类应具有xml到java绑定的详细信息

示例:

@XmlRootElement
public class Customer {
    String name;
    int id;

    public String getName() {
        return name;
    }

    @XmlElement
    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

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