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));
}
@Override
public 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);
}
结果示例: