Spring Controller @ResponseBody text / xml response UTF-8 encoding issue
I have a Spring Rest Service based annotation running on a jetty webserver (also tomcat). Controller code:
@RequestMapping(method = RequestMethod.POST, value = { "/ssrfeed/exec/",
"/query/exec" }, consumes = { "application/xml", "text/xml",
"application/x-www-form-urlencoded" }, produces = {
"application/xml;charset=UTF-8", "text/xml;charset=UTF-8",
"application/x-www-form-urlencoded;charset=UTF-8" })
@ResponseBody
protected String getXmlFeed(HttpServletRequest request,
@PathVariable String serviceName, @RequestBody String xmlReq) {
//code....
return appXMLResponse;
}
The problem is that the xml response returned by the controller contains some characters like ä ö ü (Umlaute). The response when displayed in a browser gives a parse error:
XML Parsing Error: not well-formed
Location: //localhost:8083/MySerice/ssrfeed/exec/
Line Number 18111, Column 17:
<FIRST_NAME>Tzee rfista</FIRST_NAME>
----------------^
(instead of ü, a small triangle appears)
The expected is : <FIRST_NAME>Tzeeürfista</FIRST_NAME>
I tried the solutions below, but the problem still exists.
-
Tried using filters related to the solution listed on technowobble
-
passed the encoding to StringHttpMessageConverter property
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json" /> </bean> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value="text/xml;charset=UTF-8" /> </bean> </list> </property> </bean>
-
Included
SetCharacterEncodingFilter
in tomcat -web.xml -
Changed return code
ResponseEntity
insteadString
and removed@ResponseBody
.protected ResponseEntity<String> getXmlFeed(HttpServletRequest request, @PathVariable String serviceName, @RequestBody String xmlReq) { //line of code HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.add("Content-Type", "application/xml; charset=utf-8"); return new ResponseEntity<String>(appXMLResponse, responseHeaders, HttpStatus.CREATED); }
4th solution works. But this is existing code. I am unable to change the method signature as it might affect existing clients of this service. Any ideas / pointers for solving this problem?
source to share
in the xml context of the Servlet Manager Servlet, you must add the property. eg
<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>
source to share
Finally, the problem has been resolved. Here's what I did. 1. Used constructor StringHttpMessageConverter to set encoding as:
<bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg index="0" name="defaultCharset" value="UTF-8"/>
<property name="supportedMediaTypes">
<list>
<value>application/xml</value>
<value>text/xml</value>
<value>application/x-www-form-urlencoded</value>
</list>
</property>
</bean>
Also I removed unnecessary Spring3.0 and 3.1 jars from my project. They were not required, but lay there. (should have been done earlier).
This solved the problem for me.
source to share
There is no answer that I solved the encoding issue, so I will post it.
I have a Spring RestService running on Jetty. The return piece of data retrieved from the database had the correct UTF-8 encoding, but the data from the .property file (with error and success messages) had the wrong encoding and looked like äöü ...
First I checked the encoding of the .property file itself with File-> Settings-> Editor-> Code Style-> File Encodings (this way you could not only check, but also set the desired encoding) - it was UTF-8.
Then I set the @RequestMapping responses encoding in my RestController:
@RequestMapping(value = "/category/{categoryId}", method = RequestMethod.DELETE, produces = { "application/json;**charset=UTF-8**" })
and set the defaultCharset property for Jackson2:
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes" value="application/json;" />
<property name="prettyPrint" value="true" />
<property name="defaultCharset" value="UTF-8"/>
</bean>
No result. But then I found that the problem can be solved by adding UTF-8 encoding to the PropertyPlaceholderConfigurer, which grabs data from my .property file:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:app.properties</value>
<value>classpath:database.properties</value>
<value>classpath:ru.error.messages.properties</value>
<value>classpath:ru.success.messages.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"/>
</bean>
... and the problem is gone)))
source to share