Spring controller 如下

springmvc使用StringHttpMessageConverter需要配置编码
@Controller
public class SimpleController {

    @ResponseBody
    @RequestMapping(value = "/hotel")
    public String hotel() {
        return "{\"status\":0,\"errmsg\":null,\"data\":{\"query\":\"酒店查询\",\"num\":65544,\"url\":\"www.test.com\"}}";
    }

}
springmvc使用StringHttpMessageConverter需要配置编码

 

这里使用ResponseBody, 返回值直接是一个字符串, 没有用到jackson, 如果是返回一个Object, 使用jackson转json, 是没有编码问题的

这时酒店查询几个字全都变成了问号

由于Spring处理返回值为String类型的结果时使用了StringHttpMessageConverter, 所以我们需要配置他的编码

 

springmvc使用StringHttpMessageConverter需要配置编码
    <bean >
          factory-method="forName">
        <constructor-arg value="UTF-8"/>
    </bean>

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg ref="utf8Charset"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
springmvc使用StringHttpMessageConverter需要配置编码

 





相关文章:

  • 2022-12-23
  • 2021-12-08
  • 2021-06-29
  • 2018-09-21
  • 2021-04-18
  • 2021-06-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-22
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案