package com.tuoheng.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chenjiandong
 * @description: TODO
 * @date 2022/10/27 11:40
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private LoginUserHandler loginUserHandler;

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(loginUserHandler);
    }

    /**
     * 扩展mvc框架的消息转换器
     *
     * @param converters 转换器列表集合
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建消息转换器对象
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //设置自定义的对象转换器
        converter.setObjectMapper(new OptimizationObjectMapper());
        // 通过设置索引，让自己的转换器放在最前面，否则默认的jackson转换器会在前面，用不上自己配置的转换器
        converters.add(0, converter);
    }

}
