SpringBoot版本2.2.4.RELEASE。
【1】SpringBoot接收到请求
① springboot接收到一个请求返回json格式的列表,方法参数为JSONObject 格式,使用了注解@RequestBody
为什么这里要说明返回格式、方法参数、参数注解?因为方法参数与参数注解会影响你使用不同的参数解析器与后置处理器!通常使用WebDataBinder进行参数数据绑定结果也不同。
将要调用的目标方法如下:
@ApiOperation(value="分页查询") @RequestMapping(value = "/listPage",method = RequestMethod.POST) @ResponseBody public ResponseBean listPage(@RequestBody JSONObject params){ Integer pageNum = params.getInteger("pageNum"); Integer pageSize = params.getInteger("pageSize"); String vagueParam = params.getString("vagueParam"); IPage<TbSysGoodsCategory> indexPage = new Page<>(pageNum, pageSize); QueryWrapper<TbSysGoodsCategory> queryWrapper = new QueryWrapper<>(); if (!StringUtils.isEmpty(vagueParam)){ queryWrapper.like("name",vagueParam).or().like("code",vagueParam); } //排序 queryWrapper.orderByDesc("id"); indexPage = tbSysGoodsCategoryService.page(indexPage,queryWrapper); return new ResponseBean<>(true, indexPage, CommonEnum.SUCCESS_OPTION); }
如下所示,首先进入DispatcherServlet使用RequestMappingHandlerAdapter进行处理。
而RequestMappingHandlerAdapter (extends AbstractHandlerMethodAdapter)会调用父类AbstractHandlerMethodAdapter的handle方法进行处理。
AbstractHandlerMethodAdapter.handle方法源码如下:
@Override @Nullable public final ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return handleInternal(request, response, (HandlerMethod) handler); }
可以看到RequestMappingHandlerAdapter还实现了InitializingBean接口,该接口只有一个抽象方法afterPropertiesSet
用于在BeanFactory设置完bean属性后执行,具体可参考博文:Spring – bean的初始化和销毁几种实现方式详解
② RequestMappingHandlerAdapter.handleInternal
这里首先在this.checkRequest(request)对请求进行了检测,HttpRequestMethodNotSupportedException异常就是这里抛出的。
//1.检测请求方法是否支持; //2.检测是否需要session但是没有获取到 protected final void checkRequest(HttpServletRequest request) throws ServletException { // Check whether we should support the request method. String method = request.getMethod(); if (this.supportedMethods != null && !this.supportedMethods.contains(method)) { throw new HttpRequestMethodNotSupportedException(method, this.supportedMethods); } // Check whether a session is required. if (this.requireSession && request.getSession(false) == null) { throw new HttpSessionRequiredException("Pre-existing session required but none found"); } }
其他没有什么需要特殊说明的,然后直接调用了invokeHandlerMethod方法进行实际业本文来源[email protected]搞@^&代*@码)网5务处理。