如何使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<Void> 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<Void> 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<Void> 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<ConvertiblePair> 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<Void> test(@ModelAttribute("userJson") User user) {
} 
static class User {
    private String phone;
    private String name;
}
后端接收表单2
@GetMapping("test.do")
public RestResponse<Void> 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<Void> test(@RequestParam User userJson) {  
}
@Data
static class User {
    private String phone;
    private String name;
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值