如何使SpringMVC的表单和get请求参数支持json转换?

该文章为原创(转载请注明出处):如何使SpringMVC的表单和get请求参数支持json转换? - 简书 (jianshu.com)

真实业务场景

前端请求需要表单传递json字符串或者get请求参数携带了json字符串的数据
后端只能在格式上做妥协?
例如:

前端表单
userJson:{"name":"xxx", "phone":"123"}
前端Get请求
/user/test?userJson=%7B%22name%22%3A%22xxx%22%2C%20%22phone%22%3A%22123%22%7D 
后端接收表单1
@GetMapping("test.do")
public RestResponse test(@ModelAttribute String userJson) {
    body.setUser(JSON.parseObject(userJson, User.class));
    // service.handleUser(body);
}
@Data
static class User {
    private String phone;
    private String name;
}
后端接收表单2
@GetMapping("test.do")
public RestResponse test(@ModelAttribute TestBody body) {
    body.setUser(JSON.parseObject(body.getUserJson(), User.class));
    // service.handleUser(body);
} 
@Data
static class TestBody {
    private String userJson;
}
static class User {
    private String phone;
    private String name;
}
后端接收Get请求
@GetMapping("test.do")
public RestResponse test(@RequestParam String userJson) {
    body.setUser(JSON.parseObject(userJson, User.class));
    // service.handleUser(body);
}
@Data
static class User {
    private String phone;
    private String name;
}

支持json的解决方案

利用springmvc提供的@ControllerAdvice切面,在WebDataBinder中加入使用json反序列化的方式
来进行string与各种类型的转换

package com.nuonuo.accounting.guiding.support.spring;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.util.TypeUtils;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.util.Set;

import static java.util.Collections.singleton;

/**
 * Fastjson来自动绑定参数
 * 支持表单、get请求参数
 *
 * @author uhfun
 * @see RequestMappingHandlerAdapter#initControllerAdviceCache()
 * 寻找@ControllerAdvice切面下@InitBinder注解的方法
 * @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#INIT_BINDER_METHODS
 * @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#getDataBinderFactory(org.springframework.web.method.HandlerMethod)
 * 根据切面构建InitBinderMethod方法
 * @see org.springframework.web.method.annotation.InitBinderDataBinderFactory#initBinder
 * 初始化binder时反射调用
 * @see ModelAttribute
 */
@ControllerAdvice
public class StringToAnyObjectSupport {

    private static volatile boolean INITIALIZED = false;

    @InitBinder
    public void initStringToAnyObjectConverter(WebDataBinder dataBinder) {
        GenericConversionService conversionService;
        if (dataBinder.getConversionService() instanceof GenericConversionService) {
            if (!INITIALIZED) {
                conversionService = (GenericConversionService) dataBinder.getConversionService();
                conversionService.addConverter(new StringToAnyObjectConverter());
                INITIALIZED = true;
            }
        } else {
            throw new IllegalStateException("dataBinder的ConversionService不是GenericConversionService类型实例");
        }
    }

    static class StringToAnyObjectConverter implements GenericConverter {

        @Override
        public Set getConvertibleTypes() {
            return singleton(new ConvertiblePair(String.class, Object.class));
        }

        @Override
        public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
            Class type = targetType.getType();
            if (TypeUtils.getClassFromMapping(type.getName()) != null) {
                return TypeUtils.cast(source, type, ParserConfig.getGlobalInstance());
            } else {
                return JSON.parseObject((String) source, type);
            }
        }
    }
}

如何使用?

后端接收表单1
@GetMapping("test.do")
public RestResponse test(@ModelAttribute("userJson") User user) {
} 
static class User {
    private String phone;
    private String name;
}
后端接收表单2
@GetMapping("test.do")
public RestResponse test(@ModelAttribute TestBody body) {
    User user = body.getUserJson();
    // service.handleUser(body);
} 
@Data
static class TestBody {
    private User userJson;
}
static class User {
    private String phone;
    private String name;
}
后端接收Get请求
@GetMapping("test.do")
public RestResponse test(@RequestParam User userJson) {  
}
@Data
static class User {
    private String phone;
    private String name;
}
该文章为原创(转载请注明出处):如何使SpringMVC的表单和get请求参数支持json转换? - 简书 (jianshu.com)

你可能感兴趣的:(如何使SpringMVC的表单和get请求参数支持json转换?)