【问题标题】:how to handle special characters like '<','>','&' while marshalling/unmarshalling XML in Spring 3 REST如何在 Spring 3 REST 中编组/解组 XML 时处理“<”、“>”、“&”等特殊字符
【发布时间】:2013-09-01 14:22:09
【问题描述】:

我正在使用 Spring REST,并且在从 Http 请求中解组 XML 时出现以下异常。

例外: 异常:org.springframework.http.converter.HttpMessageNotReadableException:无法读取[class com.sample.beans.Address];嵌套异常是 org.springframework.oxm.UnmarshallingFailureException: JAXB 解组异常;嵌套异常是 javax.xml.bind.UnmarshalException - 带有链接异常:[org.xml.sax.SAXParseException:实体名称必须紧跟实体引用中的“&”。]

在请求 XML 中,我传递了 '&'、'' 之类的字符 例如:安达曼和尼科巴

控制器类:

@RequestMapping(method=RequestMethod.POST,value="v1/createuser/{user}", headers="Content-Type=application/xml")
@ResponseStatus(HttpStatus.CREATED)
public ModelAndView createUser(@RequestBody Address address,@PathVariable("user") String owner,HttpServletRequest request,HttpServletResponse response){
//code to store the address and return response in the form of xml
}

Spring servlet 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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">


<context:component-scan base-package="com.samples.controller" />

<!-- To enable @RequestMapping process on type level and method level -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

<!--  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />-->
<bean id="methodHandler" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="marshallConverter"/>
        </list>
    </property>
</bean>
<bean id="marshallConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
        <property name="marshaller" ref="jaxb2Marshaller" />
        <property name="unmarshaller" ref="jaxb2Marshaller" />
        <property name="supportedMediaTypes" value="application/xml" />
    </bean>


    <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>com.sample.beans.Address</value>
              </list>
        </property>
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" />

        <bean id="dsmPolicyRuleResponse" class="org.springframework.web.servlet.view.xml.MarshallingView">
                <constructor-arg ref="jaxb2Marshaller" />
        </bean>

</beans>

但如果我通过像安达曼和尼科巴这样的东西,它就可以正常工作。 但我需要找到一种方法来处理请求中的“&”、“>”、“

【问题讨论】:

    标签: xml spring rest spring-mvc unmarshalling


    【解决方案1】:

    通过为 JAXB Marshaller 设置以下属性来完成:

    Spring Bean configuration: 
    
    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshalle r">
        <description>
            The JAXB Marshaller is used by the sendToTargetRequest to (un)marshal XML to objects and vice-versa.
        </description>
        <property name="contextPath" value="xx.orders.types:xx.orders.functionalack"/>
        <property name="mtomEnabled" value="false"/>
        <property name="marshallerProperties">
            <map>
                <entry key="jaxb.encoding">
                    <value>UTF-8</value>
                </entry>
            </map>
        </property>
    </bean>
    

    【讨论】:

    • 但是在spring mvc中,spring会负责编组/解组。如何配置spring配置做Unicode编码?
    • 我已经更新了相应的bean配置。试试这个并确认
    猜你喜欢
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 2011-01-28
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多