Spring Controller @ResponseBody text/xml响应UTF-8编码问题

Sur*_*wat 6 spring response utf-8 character-encoding resttemplate

我在jetty web服务器(还有tomcat)上运行基于注释的Spring Rest服务。控制器代码是:

\n\n
@RequestMapping(method = RequestMethod.POST, value = { "/ssrfeed/exec/",\n                "/query/exec" }, consumes = { "application/xml", "text/xml",\n                "application/x-www-form-urlencoded" }, produces = {\n                "application/xml;charset=UTF-8", "text/xml;charset=UTF-8",\n                "application/x-www-form-urlencoded;charset=UTF-8" })\n        @ResponseBody\n        protected String getXmlFeed(HttpServletRequest request,\n                @PathVariable String serviceName, @RequestBody String xmlReq) {\n\n                //code....\n                return appXMLResponse;\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

问题是控制器返回的响应 xml 包含一些字符,例如 \xc3\xa4 \xc3\xb6 \xc3\xbc (Umlaute)。在浏览器上呈现时的响应给出解析错误:

\n\n
XML Parsing Error: not well-formed\nLocation: //localhost:8083/MySerice/ssrfeed/exec/\nLine Number 18111, Column 17:\n<FIRST_NAME>Tzee rfista</FIRST_NAME>\n----------------^\n
Run Code Online (Sandbox Code Playgroud)\n\n

(\xc3\xbc 的位置出现一个小三角形)

\n\n
The expected is : <FIRST_NAME>Tzee\xc3\xbcrfista</FIRST_NAME>\n
Run Code Online (Sandbox Code Playgroud)\n\n

我已尝试以下解决方案,但问题仍然存在。

\n\n
    \n
  1. 尝试使用过滤器参考technowobble上给出的解决方案

  2. \n
  3. 将字符集传递给 StringHttpMessageConverter 属性

    \n\n
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">\n    <property name="messageConverters">\n        <list>\n            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">\n                <property name="supportedMediaTypes" value="application/json" />\n            </bean>\n            <bean class="org.springframework.http.converter.StringHttpMessageConverter">\n                <property name="supportedMediaTypes" value="text/xml;charset=UTF-8" />\n            </bean>\n        </list>\n    </property>\n</bean>\n
    Run Code Online (Sandbox Code Playgroud)\n\n
  4. \n
  5. SetCharacterEncodingFilter在 tomcat -web.xml 中启用

  6. \n
  7. 将代码更改为 returnResponseEntity而不是String并删除@ResponseBody

    \n\n
      protected ResponseEntity<String> getXmlFeed(HttpServletRequest\nrequest, @PathVariable String serviceName, @RequestBody String xmlReq) {        \n//line of code\n  HttpHeaders responseHeaders = new HttpHeaders();\n  responseHeaders.add("Content-Type", "application/xml; charset=utf-8");\n  return new ResponseEntity<String>(appXMLResponse, responseHeaders, HttpStatus.CREATED);\n\n}\n
    Run Code Online (Sandbox Code Playgroud)
  8. \n
\n\n

第四个解决方案有效,但这是现有代码,我无法更改方法签名,因为它可能会影响该服务的现有客户端。有什么想法/指针来解决这个问题吗?

\n

小智 0

在您的调度程序 servlet 上下文 xml 中,您必须添加一个属性。例如

<bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
    <array>
        <bean class = "org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
        </bean>
    </array>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)