All Downloads are FREE. Search and download functionalities are using the official Maven repository.

win.doyto.query.web.WebMvcConfigurerAdapter Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright © 2019-2024 Forb Yuan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package win.doyto.query.web;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
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.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import win.doyto.query.util.BeanUtil;
import win.doyto.query.web.component.SortArgumentResolver;
import win.doyto.query.web.config.SortFieldsProperties;
import win.doyto.query.web.config.WebComponentsConfiguration;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * WebMvcConfigurerAdapter
 *
 * @author f0rb
 */
@Import(WebComponentsConfiguration.class)
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {

    @Resource
    private SortFieldsProperties sortFieldsProperties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/favicon.ico")
                .addResourceLocations("classpath:/favicon.ico");
    }

    @Override
    public void extendMessageConverters(List> converters) {
        // 移除重复的HttpMessageConverter
        Set retain = new HashSet<>();
        List> backup = new LinkedList<>();
        for (HttpMessageConverter converter : converters) {
            String className = converter.getClass().getName();
            if (!retain.contains(className)) {
                retain.add(className);
                if (converter instanceof MappingJackson2HttpMessageConverter cvt) {
                    backup.add(0, converter);
                    configMappingJackson2HttpMessageConverter(cvt);
                } else {
                    backup.add(converter);
                }
            }
        }
        converters.clear();
        converters.addAll(backup);
    }

    protected void configMappingJackson2HttpMessageConverter(MappingJackson2HttpMessageConverter converter) {
        configMediaTypes(converter);
        configCharset(converter);
        configObjectMapper(converter.getObjectMapper());
    }

    protected void configMediaTypes(MappingJackson2HttpMessageConverter converter) {
        List supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.valueOf("application/*+json"));
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        converter.setSupportedMediaTypes(supportedMediaTypes);
    }

    protected void configCharset(MappingJackson2HttpMessageConverter converter) {
        converter.setDefaultCharset(StandardCharsets.UTF_8);
    }

    protected ObjectMapper configObjectMapper(ObjectMapper objectMapper) {
        return BeanUtil.configObjectMapper(objectMapper)
                       .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                       .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                       .setTimeZone(TimeZone.getTimeZone("GMT+8")) // 中国的东8时区
                       .setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

    @Bean
    public LocaleResolver localeResolver() {
        CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver("locale");
        cookieLocaleResolver.setDefaultLocaleFunction(request -> {
            String acceptLanguage = request.getHeader(HttpHeaders.ACCEPT_LANGUAGE);
            return StringUtils.isBlank(acceptLanguage) ? Locale.SIMPLIFIED_CHINESE : request.getLocale();
        });
        return cookieLocaleResolver;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        return new LocaleChangeInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }

    @Override
    public void addArgumentResolvers(List argumentResolvers) {
        argumentResolvers.add(new SortArgumentResolver(sortFieldsProperties));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy