HttpMessageConverter

@Bean

public HttpMessageConverter stringConverter() {

    StringHttpMessageConverter converter = new StringHttpMessageConverter(

        Charset.forName("UTF-8"));

    return converter;

  }

    /**

    * 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert

    * @return

    */

    @Bean

    public HttpMessageConverters fastJsonHttpMessageConverters() {

        // 1、需要先定义一个 convert 转换消息的对象;

        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;

        FastJsonConfig fastJsonConfig = new FastJsonConfig();

        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

        //3、在convert中添加配置信息.

        List fastMediaTypes = new ArrayList<>();

        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);      //处理中文乱码问题

        fastConverter.setSupportedMediaTypes(fastMediaTypes);

        fastConverter.setFastJsonConfig(fastJsonConfig);

        //4、将convert添加到converters当中.

        HttpMessageConverter converter = fastConverter;

        return new HttpMessageConverters(converter);

    }

你可能感兴趣的:(HttpMessageConverter)