那个网站上有做婚礼布场样图的,深圳seo优化培训,wordpress邮件找客户端,装修案例视频一、本文解决的痛点
按照大众认为的开发规范#xff0c;一般post类型的请求参数应该传在请求body里面。但是我们有些post接口只需要传入一个字段#xff0c;我们接受这种参数就得像下面这样单独创建一个类#xff0c;类中再添加要传入的基本类型字段#xff0c;配合Reques…一、本文解决的痛点
按照大众认为的开发规范一般post类型的请求参数应该传在请求body里面。但是我们有些post接口只需要传入一个字段我们接受这种参数就得像下面这样单独创建一个类类中再添加要传入的基本类型字段配合RequestBody来实现这种功能多少有点繁琐
Data
public class TextHolder {private String text;
}PostMapping(test)
public ApiResponse test(RequestBody TextHolder textHolder){....
}那么我们能不能省略类的创建实现一个类似RequestParam的注解来实现请求体参数的直接接收呢本文就是来解决这个问题的
二、实现步骤
2.1定义我们这个增强版的请求体注解
Target(ElementType.PARAMETER)
Retention(RetentionPolicy.RUNTIME)
public interface RequestBodyPlus {String value() default ;
}2.2手写一个方法参数解析器
Slf4j
public class RequestBodyPlusMethodHandler implements HandlerMethodArgumentResolver {public static final ThreadLocalMapString,Object requestBodyMap new ThreadLocal();Overridepublic boolean supportsParameter(MethodParameter methodParameter) {return methodParameter.hasParameterAnnotation(RequestBodyPlus.class);}Overridepublic Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {HttpServletRequest request nativeWebRequest.getNativeRequest(HttpServletRequest.class);RequestBodyPlus parameterAnnotation methodParameter.getParameterAnnotation(RequestBodyPlus.class);String paramName parameterAnnotation.value();if (StringUtils.isEmpty(paramName)) {paramName methodParameter.getParameterName();}MapString, Object paramsMap new HashMap();if (requestBodyMap.get() null) {String requestBodyString getRequestBodyString(request);paramsMap JSON.parseObject(requestBodyString);// 需要把请求体Map放入ThreadLocal中因为request中的inputStream读完一次下次就读不了了这也是原生的RequestBody只能在方法参数中出现一次的原因requestBodyMap.set(paramsMap);}else {paramsMap requestBodyMap.get();}Object paramValue paramsMap.get(paramName);// 有的参数需要databinder处理if (paramValue ! null webDataBinderFactory ! null) {WebDataBinder binder webDataBinderFactory.createBinder(nativeWebRequest, paramValue, paramName);paramValue binder.convertIfNecessary(paramValue, methodParameter.getParameterType(), methodParameter);}return paramValue;}private String getRequestBodyString(final ServletRequest request){StringBuilder stringBuilder new StringBuilder();try(InputStream inputStream request.getInputStream();BufferedReader bufferedReader new BufferedReader(new InputStreamReader(inputStream, Charset.forName(UTF-8)));){String line ;while((line bufferedReader.readLine()) ! null){stringBuilder.append(line);}}catch (IOException e){log.error(request的ServletInputStream转换失败,e);}finally {return stringBuilder.toString();}}}2.3需要写一个拦截器用来remove上面的threadLocal避免内存泄漏
public class RequestBodyPlusInterceptor implements HandlerInterceptor {Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {RequestBodyPlusMethodHandler.requestBodyMap.remove();}}2.4需要把拦截器和参数解析器配置好才能生效
Configuration
public class WebConfigurer extends WebMvcConfigurerAdapter {Overridepublic void addArgumentResolvers(ListHandlerMethodArgumentResolver argumentResolvers) {argumentResolvers.add(new RequestBodyPlusMethodHandler());}Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new RequestBodyPlusInterceptor());super.addInterceptors(registry);}
}三、使用案例
3.1测试代码
RestController
RequestMapping(plus)
public class TestRequestBodyPlus {PostMapping(one)public String test01(RequestBodyPlus(dx) Integer dx, RequestBodyPlus(ls) String ls, RequestBodyPlus(jk) Date jk, RequestBodyPlus(el) ListLong el) {System.out.println(el);String format %d_______%s_______%s_________%d;return String.format(format, dx, ls, jk, el.size());}//有了下面这个方法上面的接口的入参数就能传2023-11-24这种字符串InitBinder //该注解底层的源码RequestMappingHandlerAdapter#invokeHandlerMethodpublic void initBinder(WebDataBinder binder){SimpleDateFormat dateFormat new SimpleDateFormat(yyyy-MM-dd);dateFormat.setLenient(false);binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,false));}//该方法的作用域仅在当前的controller如果想全局生效需要写在ControllerAdvice所在的类中
}3.2测试请求
POST localhost:8088/plus/one
{dx : 3,ls : bbb,jk : 2023-11-24,el : [1,2,3,4,5]
}3.3测试结果
文章转载自: http://www.morning.crxdn.cn.gov.cn.crxdn.cn http://www.morning.mbdbe.cn.gov.cn.mbdbe.cn http://www.morning.pphgl.cn.gov.cn.pphgl.cn http://www.morning.lmmh.cn.gov.cn.lmmh.cn http://www.morning.mnqz.cn.gov.cn.mnqz.cn http://www.morning.gwhjy.cn.gov.cn.gwhjy.cn http://www.morning.qgtfl.cn.gov.cn.qgtfl.cn http://www.morning.gsqw.cn.gov.cn.gsqw.cn http://www.morning.sjsfw.cn.gov.cn.sjsfw.cn http://www.morning.dpjtn.cn.gov.cn.dpjtn.cn http://www.morning.nwgkk.cn.gov.cn.nwgkk.cn http://www.morning.sfnr.cn.gov.cn.sfnr.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn http://www.morning.wkpfm.cn.gov.cn.wkpfm.cn http://www.morning.fgrcd.cn.gov.cn.fgrcd.cn http://www.morning.3jiax.cn.gov.cn.3jiax.cn http://www.morning.wqwbj.cn.gov.cn.wqwbj.cn http://www.morning.kwqcy.cn.gov.cn.kwqcy.cn http://www.morning.dkgtr.cn.gov.cn.dkgtr.cn http://www.morning.hwcln.cn.gov.cn.hwcln.cn http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.fxpyt.cn.gov.cn.fxpyt.cn http://www.morning.czcbl.cn.gov.cn.czcbl.cn http://www.morning.wqbbc.cn.gov.cn.wqbbc.cn http://www.morning.qzqjz.cn.gov.cn.qzqjz.cn http://www.morning.bfjyp.cn.gov.cn.bfjyp.cn http://www.morning.fsnhz.cn.gov.cn.fsnhz.cn http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn http://www.morning.cnbdn.cn.gov.cn.cnbdn.cn http://www.morning.dqkcn.cn.gov.cn.dqkcn.cn http://www.morning.qpmwb.cn.gov.cn.qpmwb.cn http://www.morning.nytgk.cn.gov.cn.nytgk.cn http://www.morning.lywcd.cn.gov.cn.lywcd.cn http://www.morning.kkwgg.cn.gov.cn.kkwgg.cn http://www.morning.lwtfx.cn.gov.cn.lwtfx.cn http://www.morning.llyqm.cn.gov.cn.llyqm.cn http://www.morning.fyglr.cn.gov.cn.fyglr.cn http://www.morning.knrgb.cn.gov.cn.knrgb.cn http://www.morning.hxrg.cn.gov.cn.hxrg.cn http://www.morning.nhzxd.cn.gov.cn.nhzxd.cn http://www.morning.gjqwt.cn.gov.cn.gjqwt.cn http://www.morning.syfty.cn.gov.cn.syfty.cn http://www.morning.phwmj.cn.gov.cn.phwmj.cn http://www.morning.ykgkh.cn.gov.cn.ykgkh.cn http://www.morning.zlxkp.cn.gov.cn.zlxkp.cn http://www.morning.ndrzq.cn.gov.cn.ndrzq.cn http://www.morning.rpzqk.cn.gov.cn.rpzqk.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.tsycr.cn.gov.cn.tsycr.cn http://www.morning.zqkms.cn.gov.cn.zqkms.cn http://www.morning.dglszn.com.gov.cn.dglszn.com http://www.morning.nlglm.cn.gov.cn.nlglm.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.gtylt.cn.gov.cn.gtylt.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.kmqlf.cn.gov.cn.kmqlf.cn http://www.morning.cfqyx.cn.gov.cn.cfqyx.cn http://www.morning.lcdtb.cn.gov.cn.lcdtb.cn http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn http://www.morning.gmgnp.cn.gov.cn.gmgnp.cn http://www.morning.lwgsk.cn.gov.cn.lwgsk.cn http://www.morning.jtybl.cn.gov.cn.jtybl.cn http://www.morning.jzxqj.cn.gov.cn.jzxqj.cn http://www.morning.nkkpp.cn.gov.cn.nkkpp.cn http://www.morning.sqyjh.cn.gov.cn.sqyjh.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.ymdhq.cn.gov.cn.ymdhq.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.hqlnp.cn.gov.cn.hqlnp.cn http://www.morning.lwyqd.cn.gov.cn.lwyqd.cn http://www.morning.yxzfl.cn.gov.cn.yxzfl.cn http://www.morning.tfrlj.cn.gov.cn.tfrlj.cn http://www.morning.wqrk.cn.gov.cn.wqrk.cn http://www.morning.yybcx.cn.gov.cn.yybcx.cn http://www.morning.mbrbg.cn.gov.cn.mbrbg.cn http://www.morning.bmts.cn.gov.cn.bmts.cn http://www.morning.khntd.cn.gov.cn.khntd.cn