项目实战--JSON.toJSONString()
需求说明:公司使用Swagger(接口文档在线生成工具),为了让前端同事更好的了解传入参数的详细情况,应用项目中接口(eg:分页查询接口)中使用dto对象来接受前端传入的参数,但是后面中心项目中接口是用json字符串接受的,也就是说需要把dto对象转为JSON字符串,这时JSON.toJSONString()就用上了。
应用项目接口:
/** * 分页查询xxx列表 * @param pageNum 起始页 * @param pageSize 每一页的记录数 * @return RestResponse<PageInfo<BandRespDto>> */ @ApiOperation(value = "分页查询xxx信息列表", notes = "分页查询xxx信息列表") @ApiImplicitParams({ @ApiImplicitParam(name = "pageNum", value = "起始页", dataType = "integer", paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "每一页的记录数", dataType = "integer", paramType = "query") }) @GetMapping(value = {"/list"}, produces = "application/json") RestResponse<PageInfo<BandRespDto>> queryBandListByPage(
@Validated BandCreateReqDto bandCreateReqDto,
@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){ return iBandService.queryBandListByPage(bandCreateReqDto,pageNum, pageSize); }
中心项目接口:
/** * 分页查询xxx列表 * * @param filters 查询参数(json格式) * @param pageNum 分页页码 * @param pageSize 分页行数 * @return RestResponse<PageInfo<BandRespDto>> */ @GetMapping(value = {"/list"}, produces = "application/json") @ApiImplicitParams({ @ApiImplicitParam(name = "filter", value = "查询参数(json格式字符串)", dataType = "string", paramType = "query"), @ApiImplicitParam(name = "pageNum", value = "当前页码", dataType = "integer", paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "分页行数", dataType = "integer", paramType = "query") }) @ApiOperation(value = "分页查询xxx列表", notes = "分页查询xxx列表") RestResponse<PageInfo<BandRespDto>> queryBandListByPage(
@RequestParam(name = "filter") String filter,
@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize); }
使用JSON.toJSONString()
@Override public RestResponse<PageInfo<BandRespDto>> queryBandListByPage(BandCreateReqDto bandCreateReqDto, Integer pageNum, Integer pageSize) { return bandQueryApi.queryBandListByPage(JSON.toJSONString(bandCreateReqDto),pageNum, pageSize); }
希望本文章对您有帮助,您的转发、点赞是我的创作动力,十分感谢。更多好文推荐,请关注我的微信公众号--JustJavaIt