对于前端作为参数传过来的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、效果图
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/155940.html