SpringBoot 2.0 实现自定义接口参数解析器,自定义由前端传过来的Json转为java bean对象过程

8 篇文章 2 订阅
5 篇文章 0 订阅

对于前端作为参数传过来的json数据,Spring是如何转换为Java Bean的,又如何自定义这一过程呢?

Spring将参数中的json转为java Bean主要依赖于@RequestBody注解,该注解的作用原理,请看:https://www.jianshu.com/p/c1b8315c5a03

下面讲如何实现自定义这一过程:

1、首先,自定义一个注解,使用该注解标记的参数则使用自定义的参数解析器

MyRequestBody.java

package com.example.springboottest.annotation;

import java.lang.annotation.*;

/**
 * <p> 自定义参数解析注解 <p>
 *
 * @author: chen
 * @create: 2021-04-08
 **/
@Documented
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyRequestBody {

}

 2、实现自定义参数解析器

CustomJsonMethodArgumentResolver.java

package com.example.springboottest.config;

import com.example.springboottest.annotation.MyRequestBody;
import com.google.gson.Gson;
import org.apache.commons.io.IOUtils;
import org.springframework.core.MethodParameter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 * <p>  <p>
 *
 * @author: chen
 * @create: 2021-04-08
 **/
public class CustomJsonMethodArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        //被自定义注解标记的参数使用该解析器
        if(parameter.hasParameterAnnotation(MyRequestBody.class)){
            return true;
        }
        return false;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        String body = getRequestBody(webRequest);
        if(StringUtils.isEmpty(body)){
            return null;
        }
        System.out.println(body);
        //TODO 可以在这里实现一些自定操作,例如自定json的解析过程
        return new Gson().fromJson(body,parameter.getParameterType());
    }

    private String getRequestBody(NativeWebRequest webRequest) {
        HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
        try {

            return IOUtils.toString(servletRequest.getInputStream(), Charset.forName("utf-8"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
}

3、注入自定义解析器

CustomWebMvcConfigurer.java

package com.example.springboottest.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * <p>  <p>
 *
 * @author: chen
 * @create: 2021-04-08
 **/
@Configuration
public class CustomWebMvcConfigurer implements WebMvcConfigurer {

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new CustomJsonMethodArgumentResolver());
    }
}

4、测试接口

package com.example.springboottest.controller;

import com.example.springboottest.annotation.MyRequestBody;
import com.example.springboottest.entity.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>  <p>
 *
 * @author: chen
 * @create: 2021-04-08
 **/
@ResponseBody
@Controller
@RequestMapping("/test")
public class TestController {

    @PostMapping(value = "/post")
    public String testApi(@MyRequestBody Person person){
        return person.getName();
    }
}

5、效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值