Reputation: 398
I have built a Restful web service using Spring MVC 4.0.1
. The controller is returning response of every English words and characters in a browser. But every individual character that are NON-ENGLISH
are being returned as ?
.
For e.g. for नेपाल
(Nepali word pronouncing Nepal
) it is returning as ????
.
Here's my RestController
class:
@RestController
public class TestController {
private final ApplicationContext applicationContext;
@Autowired
public TestController(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@RequestMapping(value = {"/test/{test}"})
public String getLatestCategoryNews(
@PathVariable("categories") String categories,
@RequestParam String test)
{
return "नेपाल";
}
}
Here's my dispatcher-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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.billions.news.web.controller"/>
<mvc:annotation-driven />
Here's my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
Here's my application-context.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd>
<context:property-placeholder location="application.properties"/>
<context:component-scan base-package="com.test.web"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
When I return reponse converting it to the json string using json-io, in IDE console I can get the desired text. But when I receive the response in Browser it gets converted back to '????'. It annoys me a lot.
I used following too on @RequesMapping
:
method = RequestMethod.GET,
produces = MediaType.ALL_VALUE
But it still produces the same response with '????'.
I tried loads of solution out there on the web including filters
. But none of them helped.
I am using tomcat as a server.
Is there a way so that I can get response in any language out there including English in spring-mvc restcontroller.
EDIT:
Following solved the issue: All I had to do was to replace
<mvc:annotation-driven />
with
<mvc:annotation-driven >
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
Upvotes: 0
Views: 256
Reputation: 4230
first of all, in your controller's method getLatestCategoryNews
you are returning String
, this means that spring will pick one of the registered HttpMessageConverters
which in your case will be StringHttpMessageConverer
(here is the doc) which will be responsible for writing the returned String
.
By default charset for this StringHttpMessageConverer
default constructor that uses "ISO-8859-1" as the default charset.
you should override this message converter's charset.
simply register bean:
<beans class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<array>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
</bean>
</array>
</property>
</beans>
UPDATE from me
you should also set project encoding in maven pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Update from @Computergodzilla
I had to do was to remove <mvc:annotation-driven/>
with
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
Upvotes: 1