Spring Restful服务只返回英文字符,但除英文外的其他字符显示为'?'

时间:2015-12-11 19:23:04

标签: spring-mvc spring-restcontroller

我使用Spring MVC 4.0.1构建了Restful Web服务。控制器返回浏览器中每个英文单词和字符的响应。但是NON-ENGLISH的每个字符都将作为?返回。 对于例如对于नेपाल(尼泊尔语发音Nepal),它将返回????

这是我的RestController课程:

@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 "नेपाल";
}
}

这是我的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 />

这是我的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>

这是我的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" />

当我使用json-io返回响应将其转换为json字符串时,在IDE控制台中我可以获得所需的文本。但是当我在浏览器中收到回复时,它会被转换回&#39; ????&#39;。这让我很烦恼。

我在@RequesMapping使用了以下内容:

method = RequestMethod.GET,
        produces = MediaType.ALL_VALUE

但它仍然会产生相同的响应&#39; ????&#39;。

我在网上尝试了大量解决方案,包括filters。但他们都没有帮助。

我使用tomcat作为服务器。

有没有办法让我能用任何语言得到回应,包括spring-mvc restcontroller中的英语。

编辑:

以下解决了这个问题: 我所要做的就是替换

<mvc:annotation-driven />

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

1 个答案:

答案 0 :(得分:1)

首先,在您的控制器方法getLatestCategoryNews中,您将返回String,这意味着spring将选择一个已注册的HttpMessageConverters,在您的情况下StringHttpMessageConverer }(here是doc),负责编写返回的String

StringHttpMessageConverer

的默认字符集
  

使用“ISO-8859-1”作为默认值的默认构造函数   字符集。

你应该覆盖这个消息转换器的字符集。

只需注册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>

向我提出更新

您还应该在maven pom.xml中设置项目编码

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

从@Computergodzilla更新

我必须做的是用

删除<mvc:annotation-driven/>
<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>
相关问题