2013-08-29 1 views
1

Spring REST를 사용하고 있으며 HTTP 요청에서 XML을 언 마샬링하는 중 다음 예외가 발생합니다.Spring 3에서 XML을 정렬/비 정렬하는 동안 '<','>', '&'와 같은 특수 문자를 처리하는 방법 REST

예외 : 예외 : org.springframework.http.converter.HttpMessageNotReadableException : [클래스 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 
} 

봄 서블릿 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> 

하지만 Andaman & Nicobar와 같은 것을 전달하면 정상적으로 작동합니다. 는하지만 요청에 '<'을 '&', '>'와 같은 특수 문자를 처리 할 수있는 방법을 찾을 필요가

답변

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> 
+1

그러나 봄 mvc 봄, 마샬링/unmarshalling.How 유니 코드 인코딩을 할 봄 구성을 구성 돌볼 것인가? – Ravikiran

+0

해당 콩 구성을 업데이트했습니다. 이것을 시도하고 그것을 확인하십시오 –

관련 문제