接口返回数据与当前做兼容

This commit is contained in:
tianliyong 2022-11-02 17:16:52 +08:00
parent 28e15fe975
commit 7af650deb5
11 changed files with 236 additions and 58 deletions

View File

@ -84,6 +84,11 @@ public class ApplicationReadyInfoAutoConfiguration {
s.append("\tSwaggerUI:\t\t").append(protocol).append("://").append(ip)
.append(":").append(port).append("/swagger-ui.html\n");
}
String knife4jPageLocation = "classpath:META-INF/resources/doc.html";
if (Seq.of(activeProfiles).contains("swagger") && Resources.exists(knife4jPageLocation)) {
s.append("\tSwaggerUI:\t\t").append(protocol).append("://").append(ip)
.append(":").append(port).append("/doc.html\n");
}
}
s.append("\tProfile(s):\t\t").append(Arrays.toString(activeProfiles)).append("\n");
s.append("----------------------------------------------------------------");

View File

@ -1,17 +1,18 @@
package cn.axzo.framework.autoconfigure.jackson;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.USE_DEFAULTS;
import static com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER;
import static com.fasterxml.jackson.databind.DeserializationFeature.ACCEPT_FLOAT_AS_INT;
import static com.fasterxml.jackson.databind.DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY;
import static com.fasterxml.jackson.databind.DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS;
import static com.fasterxml.jackson.databind.MapperFeature.PROPAGATE_TRANSIENT_MARKER;
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS;
import java.util.TimeZone;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.core.Ordered;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import java.util.TimeZone;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_ABSENT;
import static com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER;
import static com.fasterxml.jackson.databind.DeserializationFeature.*;
import static com.fasterxml.jackson.databind.MapperFeature.PROPAGATE_TRANSIENT_MARKER;
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS;
/**
* @Description
* @Author liyong.tian
@ -22,7 +23,7 @@ public class JacksonCustomer implements Jackson2ObjectMapperBuilderCustomizer, O
@Override
public void customize(Jackson2ObjectMapperBuilder builder) {
builder.serializationInclusion(NON_ABSENT);
builder.serializationInclusion(USE_DEFAULTS);
builder.timeZone(TimeZone.getDefault());
// disable

View File

@ -49,7 +49,6 @@ public class PageImpl<T> implements Page<T> {
}
return new PageImpl<>(result, current, total);
}
@Nonnull
@Override
public <S> Page<S> mapAll(Function<List<T>, List<S>> mapper) {

View File

@ -16,10 +16,10 @@ import java.util.stream.Stream;
@Description("基础错误码")
public enum BaseCode implements IModuleRespCode{
SUCCESS("000", "成功", 200) {
SUCCESS("000", "success", 200) {
@Override
public String getRespCode() {
return "0";
return "200";
}
},
BAD_REQUEST("400", "请求参数错误", 400),

View File

@ -10,7 +10,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import java.beans.Transient;
import java.util.LinkedHashMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@ -23,8 +23,8 @@ import static jodd.util.StringUtil.isNotBlank;
* @Date 2020/9/7 20:28
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
@AllArgsConstructor
@JsonPropertyOrder({"code", "msg", "data"})
public abstract class ApiCoreResult<E> implements Result {
@ -74,7 +74,7 @@ public abstract class ApiCoreResult<E> implements Result {
@Override
public Map<String, Object> toMap() {
Map<String, Object> map = new LinkedHashMap<>();
Map<String, Object> map = new HashMap<>();
map.put("code", code);
map.put("msg", msg);
map.put("data", data);

View File

@ -60,7 +60,7 @@ public class ApiListResult<E> extends ApiCoreResult<List<E>> {
return new ApiListResult<>(code, message, data);
}
@ConstructorProperties({"code", "message", "data"})
@ConstructorProperties({"code", "msg", "data"})
public ApiListResult(String code, String message, List<E> data) {
super(code, message, data);
}

View File

@ -1,18 +1,12 @@
package cn.axzo.framework.domain.web.result;
import cn.axzo.framework.domain.page.Page;
import cn.axzo.framework.domain.page.PageImpl;
import cn.axzo.framework.domain.page.PageResp;
import cn.axzo.framework.domain.page.PageVerbose;
import cn.axzo.framework.domain.page.Pageable;
import cn.axzo.framework.domain.page.*;
import cn.axzo.framework.domain.web.code.IRespCode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import java.beans.ConstructorProperties;
@ -29,22 +23,9 @@ import static com.google.common.collect.Lists.newArrayList;
* @Date 2020/9/7 20:32
**/
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@JsonPropertyOrder({"total", "code", "msg", "data", "pageNum", "pageSize", "verbose"})
public class ApiPageResult<E> extends ApiCoreResult<List<E>> {
@ApiModelProperty(value = "总记录数", position = 101, required = true)
private final Long total;
@ApiModelProperty(value = "当前页", position = 102)
private final Integer pageNum;
@ApiModelProperty(value = "每页显示数量", position = 103)
private final Integer pageSize;
@ApiModelProperty(value = "分页冗余信息", position = 104)
private final PageVerbose verbose;
@JsonPropertyOrder({"code", "msg", "data"})
public class ApiPageResult<E> extends ApiCoreResult<PageData<E>>{
public static <E> ApiPageResult<E> empty() {
return ok(newArrayList(), 0L);
@ -90,7 +71,7 @@ public class ApiPageResult<E> extends ApiCoreResult<List<E>> {
public static <E> ApiPageResult<E> ok(List<E> data, Long total) {
return build(total, SUCCESS.getRespCode(), SUCCESS.getMessage(), data,
null, null, null);
null, null, null);
}
public static <E> ApiPageResult<E> ok(List<E> data, Long total, Integer pageNumber, Integer pageSize) {
@ -98,7 +79,7 @@ public class ApiPageResult<E> extends ApiCoreResult<List<E>> {
}
public static <E> ApiPageResult<E> ok(List<E> data, Long total, Integer pageNumber, Integer pageSize,
PageVerbose verbose) {
PageVerbose verbose) {
return build(total, SUCCESS.getRespCode(), SUCCESS.getMessage(), data, pageNumber, pageSize, verbose);
}
@ -119,28 +100,31 @@ public class ApiPageResult<E> extends ApiCoreResult<List<E>> {
}
public static <E> ApiPageResult<E> build(Long total, IRespCode code, List<E> data,
Integer pageNum, Integer pageSize) {
Integer pageNum, Integer pageSize) {
return build(total, code.getRespCode(), code.getMessage(), data, pageNum, pageSize, null);
}
public static <E> ApiPageResult<E> build(Long total, String code, String message, List<E> data,
Integer pageNum, Integer pageSize) {
Integer pageNum, Integer pageSize) {
return new ApiPageResult<>(total, code, message, data, pageNum, pageSize, null);
}
public static <E> ApiPageResult<E> build(Long total, String code, String message, List<E> data,
Integer pageNum, Integer pageSize, PageVerbose verbose) {
Integer pageNum, Integer pageSize, PageVerbose verbose) {
return new ApiPageResult<>(total, code, message, data, pageNum, pageSize, verbose);
}
@ConstructorProperties({"total", "code", "message", "data", "pageNum", "pageSize", "verbose"})
public ApiPageResult(Long total, String code, String message, List<E> data, Integer pageNum, Integer pageSize,
@ConstructorProperties({"code", "msg", "data"})
public ApiPageResult(Long total, String code, String message, List<E> list, Integer pageNum, Integer pageSize,
PageVerbose verbose) {
super(code, message, data);
this.total = total;
this.pageNum = pageNum;
this.pageSize = pageSize;
this.verbose = verbose;
PageData<E> data = new PageData<E>();
data.setList(list);
data.setTotalCount(total);
data.setPageNum(pageNum);
data.setPageSize(pageSize);
this.code = code;
this.msg = message;
this.data = data;
}
@Override
@ -148,15 +132,11 @@ public class ApiPageResult<E> extends ApiCoreResult<List<E>> {
Map<String, Object> map = new LinkedHashMap<>();
map.put("code", code);
map.put("msg", msg);
map.put("total", total);
map.put("data", data);
map.put("pageNum", pageNum);
map.put("pageSize", pageSize);
map.put("verbose", verbose);
return map;
}
public Page<E> toPage(Pageable pageable) {
return new PageImpl<>(data == null ? Lists.newArrayList() : data, pageable, total);
return new PageImpl<>(data == null ? Lists.newArrayList() : data.getList(), pageable, data.getTotalCount());
}
}

View File

@ -0,0 +1,161 @@
package cn.axzo.framework.domain.web.result;
import static cn.axzo.framework.domain.web.code.BaseCode.SUCCESS;
import static com.google.common.collect.Lists.newArrayList;
import cn.axzo.framework.domain.page.Page;
import cn.axzo.framework.domain.page.PageImpl;
import cn.axzo.framework.domain.page.PageResp;
import cn.axzo.framework.domain.page.PageVerbose;
import cn.axzo.framework.domain.page.Pageable;
import cn.axzo.framework.domain.web.code.IRespCode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModelProperty;
import java.beans.ConstructorProperties;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
/**
* @Description
* @Author liyong.tian
* @Date 2020/9/7 20:32
**/
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@JsonPropertyOrder({"total", "code", "msg", "data", "pageNum", "pageSize", "verbose"})
public class ApiPageResult1<E> extends ApiCoreResult<List<E>> {
@ApiModelProperty(value = "总记录数", position = 101, required = true)
private final Long total;
@ApiModelProperty(value = "当前页", position = 102)
private final Integer pageNum;
@ApiModelProperty(value = "每页显示数量", position = 103)
private final Integer pageSize;
@ApiModelProperty(value = "分页冗余信息", position = 104)
private final PageVerbose verbose;
public static <E> ApiPageResult1<E> empty() {
return ok(newArrayList(), 0L);
}
public static <E> ApiPageResult1<E> ok(Page<E> page) {
Pageable current = page.current();
// data
List<E> data = page.getContent();
if (!current.needContent() && data.isEmpty()) {
data = null;
}
// pageNum & pageSize
Integer pageNum = null;
Integer pageSize = null;
if (current.isFixEdge() && current.needContent()) {
pageNum = current.getPageNumber();
pageSize = current.getPageSize();
}
// verbose
PageVerbose verbose = PageVerbose.of(page).orElse(null);
return ok(data, page.getTotal(), pageNum, pageSize, verbose);
}
public static <E> ApiPageResult1 ok(PageInfo<E> pageInfo) {
return ok(pageInfo.getList(), pageInfo.getTotal(), pageInfo.getPageNum(), pageInfo.getPageSize());
}
public static <E> ApiPageResult1<E> ok(org.springframework.data.domain.Page<E> page) {
return ok(page.getContent(), page.getTotalElements());
}
public static <E> ApiPageResult1<E> ok(IPage<E> page) {
return ok(page.getRecords(), page.getTotal(), (int)page.getCurrent(), (int)page.getSize());
}
public static <E> ApiPageResult1<E> ok(PageResp<E> page) {
return ok(page.getList(), page.getTotalCount(), page.getPage().intValue(), page.getPageSize().intValue());
}
public static <E> ApiPageResult1<E> ok(List<E> data, Long total) {
return build(total, SUCCESS.getRespCode(), SUCCESS.getMessage(), data,
null, null, null);
}
public static <E> ApiPageResult1<E> ok(List<E> data, Long total, Integer pageNumber, Integer pageSize) {
return build(total, SUCCESS.getRespCode(), SUCCESS.getMessage(), data, pageNumber, pageSize, null);
}
public static <E> ApiPageResult1<E> ok(List<E> data, Long total, Integer pageNumber, Integer pageSize,
PageVerbose verbose) {
return build(total, SUCCESS.getRespCode(), SUCCESS.getMessage(), data, pageNumber, pageSize, verbose);
}
public static <E> ApiPageResult1<E> err(IRespCode code) {
return build(code);
}
public static <E> ApiPageResult1<E> err(IRespCode code, String message) {
return err(code.getRespCode(), message);
}
public static <E> ApiPageResult1<E> err(String code, String message) {
return build(null, code, message, null, null, null, null);
}
public static <E> ApiPageResult1<E> build(IRespCode code) {
return build(null, code, null, null, null);
}
public static <E> ApiPageResult1<E> build(Long total, IRespCode code, List<E> data,
Integer pageNum, Integer pageSize) {
return build(total, code.getRespCode(), code.getMessage(), data, pageNum, pageSize, null);
}
public static <E> ApiPageResult1<E> build(Long total, String code, String message, List<E> data,
Integer pageNum, Integer pageSize) {
return new ApiPageResult1<>(total, code, message, data, pageNum, pageSize, null);
}
public static <E> ApiPageResult1<E> build(Long total, String code, String message, List<E> data,
Integer pageNum, Integer pageSize, PageVerbose verbose) {
return new ApiPageResult1<>(total, code, message, data, pageNum, pageSize, verbose);
}
@ConstructorProperties({"total", "code", "message", "data", "pageNum", "pageSize", "verbose"})
public ApiPageResult1(Long total, String code, String message, List<E> data, Integer pageNum, Integer pageSize,
PageVerbose verbose) {
super(code, message, data);
this.total = total;
this.pageNum = pageNum;
this.pageSize = pageSize;
this.verbose = verbose;
}
@Override
public Map<String, Object> toMap() {
Map<String, Object> map = new LinkedHashMap<>();
map.put("code", code);
map.put("msg", msg);
map.put("total", total);
map.put("data", data);
map.put("pageNum", pageNum);
map.put("pageSize", pageSize);
map.put("verbose", verbose);
return map;
}
public Page<E> toPage(Pageable pageable) {
return new PageImpl<>(data == null ? Lists.newArrayList() : data, pageable, total);
}
}

View File

@ -0,0 +1,33 @@
package cn.axzo.framework.domain.web.result;
import cn.axzo.framework.domain.page.PageVerbose;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PageData<E> {
@ApiModelProperty(value = "列表", position = 100, required = true)
private List<E> list;
@ApiModelProperty(value = "总记录数", position = 101, required = true)
private Long totalCount;
@ApiModelProperty(value = "当前页", position = 102)
private Integer pageNum;
@ApiModelProperty(value = "每页显示数量", position = 103)
private Integer pageSize;
// @ApiModelProperty(value = "分页冗余信息", position = 104)
// private PageVerbose verbose;
}

View File

@ -37,13 +37,11 @@
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,6 +1,7 @@
package cn.axzo.framework.web.http;
import cn.axzo.framework.domain.page.PageResp;
import cn.axzo.framework.domain.web.result.PageData;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import cn.axzo.framework.core.net.Nets;
@ -20,7 +21,7 @@ import java.util.List;
* @author liyong.tian
* @since 2017/11/14 下午7:52
*/
public class ApiPageEntity<E> extends ApiCoreEntity<List<E>, ApiPageResult<E>> {
public class ApiPageEntity<E> extends ApiCoreEntity<PageData<E>, ApiPageResult<E>> {
/**
* Create a new {@code HttpEntity} with the given body, headers, and status code.