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

cn.mklaus.framework.web.CustomWebMvcConfigurer Maven / Gradle / Ivy

package cn.mklaus.framework.web;

import com.alibaba.fastjson.JSONArray;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.Formatter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.text.ParseException;
import java.util.Locale;
import java.util.Objects;

/**
 * @author klaus
 * Created on 2019-08-06 18:09
 */
@Configuration
public class CustomWebMvcConfigurer implements WebMvcConfigurer {

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

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new Formatter() {
            @Override
            public JSONArray parse(String s, Locale locale) throws ParseException {
                if (Objects.isNull(s)) {
                    return new JSONArray();
                }
                return JSONArray.parseArray(s);
            }
            @Override
            public String print(JSONArray objects, Locale locale) {
                return objects.toJSONString();
            }
        });
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy