package com.chaozhanggui.common.system.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.Iterator;
import java.util.List;

@Configuration
public class MyMvcConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
        while (iterator.hasNext()) {
            HttpMessageConverter<?> next = iterator.next();
            if (next.getClass() == MappingJackson2HttpMessageConverter.class || next.getClass() == StringHttpMessageConverter.class) {
                iterator.remove();
            }
        }
        converters.add(0, new MappingJackson2HttpMessageConverter());//使其优先于StringHttpMessageConverter,以使返回类型为String的值会被前者处理，否则String不会被转为json
    }

}
