java自定义返回结果

编程入门 行业动态 更新时间:2024-10-27 08:38:35

java<a href=https://www.elefans.com/category/jswz/34/1771438.html style=自定义返回结果"/>

java自定义返回结果

java自定义返回结果

结果集设置Result

@Data
public class Result<T> implements Serializable {private static final long serialVersionUID = -6107682542287491755L;@ApiModelProperty(value = "返回码", example = "200")private Integer code = 200;@ApiModelProperty(value = "返回消息", example = "")private String message = "SUCCESS";@ApiModelProperty(value = "返回数据", example = "")private T data;private Result() {}public Result(T data) {this.data = data;}public Result(Integer code, String message) {this.code = code;this.message = message;}}

分页返回参数设置

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.io.Serializable;
import java.util.List;/*** @description 返回分页VO**/
@Data
@ApiModel
@NoArgsConstructor
@AllArgsConstructor
public class PageInfoVO<T> implements Serializable {private static final long serialVersionUID = -7278102848592590622L;@ApiModelProperty(value = "页码")private Integer page;@ApiModelProperty(value = "分页大小")private Integer size;@ApiModelProperty(value = "总条数")private Long total;private List<T> list;public PageInfoVO(Long total, List<T> list) {this.total = total;this.list = list;}public static <T> PageInfoVO<T> to(IPage<?> source, Class<T> clazz) {Integer current = Math.toIntExact(source.getCurrent());Integer sizePage = Math.toIntExact(source.getSize());return new PageInfoVO(current, sizePage, source.getTotal(), JSON.parseArray(JSON.toJSONString(source.getRecords()), clazz));}
}

使用

    @PostMapping("/activityList")@ApiOperation("列表")public Result<PageInfoVO<PtExamBagVO>> getExamBagList(@RequestBody @Validated PtExamBagQuery query) {return new Result<>(ptExamRecoveryLogService.getExamBagList(query));}@Overridepublic PageInfoVO<PtExamBagVO> getExamBagList(PtExamBagQuery query) {IPage<PtExamBagVO> page = ptExamRecoveryLogMapper.getExamBagList(new Page<>(query.getPage(), query.getSize()), query);if (CollectionUtil.isNotEmpty(page.getRecords())) {page.getRecords().forEach(x -> {int outboundCount = ptExamActivityService.count(new LambdaQueryWrapper<PtExamActivity>().eq(PtExamActivity::getOrgCode, x.getOrgCode()).eq(PtExamActivity::getTestpaperName, x.getTestpaperName()));int recoveryCount = ptExamActivityService.count(new LambdaQueryWrapper<PtExamActivity>().eq(PtExamActivity::getOrgCode, x.getOrgCode()).eq(PtExamActivity::getTestpaperName, x.getTestpaperName()).eq(PtExamActivity::getIsRecovery, 1));x.setOutbound(outboundCount);x.setRecovery(recoveryCount);x.setOutbound(outboundCount - recoveryCount);});}return PageInfoVO.to(page, PtExamBagVO.class);}

结果示例:

更多推荐

java自定义返回结果

本文发布于:2023-12-03 12:41:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1655460.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!