0

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>
Mubin Shrestha
  • 398
  • 3
  • 22

1 Answers1

1

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>
Nikolay Rusev
  • 4,060
  • 3
  • 19
  • 29
  • Thank you Nikolay. Isn't the first tag should be instead of . Tomcat threw an error 'Caused by: org.xml.sax.SAXParseException; lineNumber: 21; columnNumber: 98; cvc-complex-type.3.2.2: Attribute 'class' is not allowed to appear in element 'beans'.'. But still I am getting ??, when I registered above bean. – Mubin Shrestha Dec 12 '15 at 14:33
  • Sorry to say, I am not using maven. :( – Mubin Shrestha Dec 12 '15 at 14:46
  • 1
    which version you are using? i think spring cannot find registered message converter. see this link - http://spring.io/blog/2011/02/21/spring-3-1-m1-mvc-namespace-enhancements-and-configuration/ - Registration of HttpMessageConverters – Nikolay Rusev Dec 12 '15 at 14:53
  • Thank you Nikolay. The link helped. All I had to do was to remove with ' '. Please update the above answer with above snippet and I will accept and upvote your answer. Thank you. You are awesome. – Mubin Shrestha Dec 12 '15 at 15:08