当不使用Jackson时,Spring将默认Content-type设置为“application / json; charset = utf-8”

时间:2014-08-12 22:42:52

标签: spring spring-mvc

我想更改我的春季应用默认"内容类型" to" application / json; charset = utf-8"而不只是" application / json"

4 个答案:

答案 0 :(得分:3)

@Configuration
@EnableWebMvc
public class MVCConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureContentNegotiation(
         ContentNegotiationConfigurer configurer) {
        final Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("charset", "utf-8");

        configurer.defaultContentType(new MediaType(
          MediaType.APPLICATION_JSON, parameterMap));
    }
}

答案 1 :(得分:3)

春天&gt; 4.3.4

@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

答案 2 :(得分:0)

修改生成

例如:

@RequestMapping(method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })

public @ResponseBody Object get1() {
...
}

答案 3 :(得分:0)

通过使用请求过滤器,我找到了默认内容类型字符集的最简单解决方案:

@Component
public class CharsetRequestFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");

        filterChain.doFilter(request, response);
    }
}