首页 > 解决方案 > 俄语字符显示为 ??? 在 Spring-MVC 中

问题描述

所以我创建了一个控制器

@Controller
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping("/all")
    @ResponseBody
    public String display()
    {
        return "Мегафон Игры";  //russian characters
    }   
}

现在,当我点击 url http://localhost:8080/SpringMVC/hello/all 时,我得到??????? ????了响应。

我在tomcat的server.xml文件中配置了URIEncoding,如下所示

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>` 
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>

我已经在 Chrome 浏览器、邮递员和 Eclipse 控制台上测试了我的响应。

我什至尝试在 web.xml 文件中添加编码过滤器

<filter>
    <filter-name>encoding-filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
       <param-name>encoding</param-name>
       <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
       <param-name>forceEncoding</param-name>
       <param-value>FALSE</param-value>
    </init-param>
 </filter>
 <filter-mapping>
    <filter-name>encoding-filter</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>

请帮帮我我错过了什么?

标签: javaspring-mvctomcatservletsutf-8

解决方案


可能是您需要定义响应正文的编码。
为此,您可以使用@RequestMapping注释,例如

@RequestMapping(
     value = "/all", 
     method = RequestMethod.GET,
     produces = MediaType.APPLICATION_JSON_UTF8_VALUE
)

推荐阅读