1

我正在使用 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 中,我传递了像 '&'、'<'、'>' 这样的字符 例如:Andaman & Nicobar

控制器类:

@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
}

春季小服务程序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>

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

4

1 回答 1

3

通过为 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>
于 2013-08-29T12:44:01.960 回答