响应类型封装优化
This commit is contained in:
@@ -152,9 +152,9 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
|
|||||||
*/
|
*/
|
||||||
@Operation(summary = "信息", description = "根据ID查询单个信息")
|
@Operation(summary = "信息", description = "根据ID查询单个信息")
|
||||||
@GetMapping("/info")
|
@GetMapping("/info")
|
||||||
protected R info(@RequestAttribute() JSONObject requestParams,
|
protected R<T> info(@RequestAttribute() JSONObject requestParams,
|
||||||
@RequestParam() Long id) {
|
@RequestParam() Long id) {
|
||||||
return R.ok(service.info(requestParams, id));
|
return R.ok((T) service.info(requestParams, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -164,7 +164,7 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
|
|||||||
*/
|
*/
|
||||||
@Operation(summary = "查询", description = "查询多个信息")
|
@Operation(summary = "查询", description = "查询多个信息")
|
||||||
@PostMapping("/list")
|
@PostMapping("/list")
|
||||||
protected R list(@RequestAttribute() JSONObject requestParams,
|
protected R<List<T>> list(@RequestAttribute() JSONObject requestParams,
|
||||||
@RequestAttribute(COOL_LIST_OP) CrudOption<T> option) {
|
@RequestAttribute(COOL_LIST_OP) CrudOption<T> option) {
|
||||||
QueryModeEnum queryModeEnum = option.getQueryModeEnum();
|
QueryModeEnum queryModeEnum = option.getQueryModeEnum();
|
||||||
List list = (List) switch (queryModeEnum) {
|
List list = (List) switch (queryModeEnum) {
|
||||||
@@ -183,7 +183,7 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
|
|||||||
*/
|
*/
|
||||||
@Operation(summary = "分页", description = "分页查询多个信息")
|
@Operation(summary = "分页", description = "分页查询多个信息")
|
||||||
@PostMapping("/page")
|
@PostMapping("/page")
|
||||||
protected R page(@RequestAttribute() JSONObject requestParams,
|
protected R<PageResult<T>> page(@RequestAttribute() JSONObject requestParams,
|
||||||
@RequestAttribute(COOL_PAGE_OP) CrudOption<T> option) {
|
@RequestAttribute(COOL_PAGE_OP) CrudOption<T> option) {
|
||||||
Integer page = requestParams.getInt("page", 1);
|
Integer page = requestParams.getInt("page", 1);
|
||||||
Integer size = requestParams.getInt("size", 20);
|
Integer size = requestParams.getInt("size", 20);
|
||||||
@@ -218,15 +218,8 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
|
|||||||
*
|
*
|
||||||
* @param page 分页返回数据
|
* @param page 分页返回数据
|
||||||
*/
|
*/
|
||||||
protected PageResult pageResult(Page page) {
|
protected PageResult<T> pageResult(Page<T> page) {
|
||||||
PageResult result = new PageResult();
|
return PageResult.of( page );
|
||||||
Map<String, Object> pagination = new HashMap<>();
|
|
||||||
pagination.put("size", page.getPageSize());
|
|
||||||
pagination.put("page", page.getPageNumber());
|
|
||||||
pagination.put("total", page.getTotalRow());
|
|
||||||
result.setList(page.getRecords());
|
|
||||||
result.setPagination(pagination);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Class<T> currentEntityClass() {
|
public Class<T> currentEntityClass() {
|
||||||
|
|||||||
@@ -21,10 +21,10 @@ public class CoolExceptionHandler {
|
|||||||
public R handleRRException(CoolException e) {
|
public R handleRRException(CoolException e) {
|
||||||
R r = new R();
|
R r = new R();
|
||||||
if (ObjUtil.isNotEmpty(e.getData())) {
|
if (ObjUtil.isNotEmpty(e.getData())) {
|
||||||
r.put("data", e.getData());
|
r.setData( e.getData() );
|
||||||
} else {
|
} else {
|
||||||
r.put("code", e.getCode());
|
r.setCode( e.getCode() );
|
||||||
r.put("message", e.getMessage());
|
r.setMessage( e.getMessage() );
|
||||||
}
|
}
|
||||||
if (ObjUtil.isNotEmpty(e.getCause())) {
|
if (ObjUtil.isNotEmpty(e.getCause())) {
|
||||||
log.error(e.getCause().getMessage(), e.getCause());
|
log.error(e.getCause().getMessage(), e.getCause());
|
||||||
|
|||||||
@@ -1,11 +1,33 @@
|
|||||||
package com.cool.core.request;
|
package com.cool.core.request;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import com.mybatisflex.core.paginate.Page;
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class PageResult {
|
@Schema( title = "分页数据模型" )
|
||||||
private List<?> list;
|
public class PageResult<T> {
|
||||||
private Map<String, Object> pagination;
|
@Schema( title = "分页数据" )
|
||||||
|
private List<T> list;
|
||||||
|
private Pagination pagination = new Pagination();
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Pagination {
|
||||||
|
@Schema( title = "页码" )
|
||||||
|
private Long page;
|
||||||
|
@Schema( title = "本页数量" )
|
||||||
|
private Long size;
|
||||||
|
@Schema( title = "总页数" )
|
||||||
|
private Long total;
|
||||||
|
}
|
||||||
|
|
||||||
|
static public <B> PageResult<B> of(Page<B> page ){
|
||||||
|
PageResult<B> result = new PageResult<B>();
|
||||||
|
result.setList(page.getRecords());
|
||||||
|
result.pagination.setPage( page.getPageNumber() );
|
||||||
|
result.pagination.setSize( page.getPageSize() );
|
||||||
|
result.pagination.setTotal( page.getTotalRow() );
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,42 @@
|
|||||||
package com.cool.core.request;
|
package com.cool.core.request;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回信息
|
* 返回信息
|
||||||
*/
|
*/
|
||||||
public class R extends HashMap<String, Object> {
|
@Schema(title = "响应数据结构")
|
||||||
|
@Data
|
||||||
|
public class R<T> implements Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
|
||||||
|
@Schema(title = "编码:1000表示成功,其他值表示失败")
|
||||||
|
private int code = 1000;
|
||||||
|
|
||||||
|
@Schema(title = "消息内容")
|
||||||
|
private String message = "success";
|
||||||
|
|
||||||
|
@Schema(title = "响应数据")
|
||||||
|
private T data;
|
||||||
|
|
||||||
|
@Schema(title = "响应数据")
|
||||||
|
private Map<String, Object> dataMap = new HashMap<String, Object>();
|
||||||
|
|
||||||
|
|
||||||
public R() {
|
public R() {
|
||||||
put("code", 1000);
|
|
||||||
put("message", "success");
|
}
|
||||||
|
|
||||||
|
public R( int code, String message, T data ) {
|
||||||
|
this.code = code;
|
||||||
|
this.message = message;
|
||||||
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static R error() {
|
public static R error() {
|
||||||
@@ -23,14 +49,14 @@ public class R extends HashMap<String, Object> {
|
|||||||
|
|
||||||
public static R error(int code, String msg) {
|
public static R error(int code, String msg) {
|
||||||
R r = new R();
|
R r = new R();
|
||||||
r.put("code", code);
|
r.code = code;
|
||||||
r.put("message", msg);
|
r.message = msg;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static R okMsg(String msg) {
|
public static R okMsg(String msg) {
|
||||||
R r = new R();
|
R r = new R();
|
||||||
r.put("message", msg);
|
r.message = msg;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,12 +64,21 @@ public class R extends HashMap<String, Object> {
|
|||||||
return new R();
|
return new R();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static R ok(Object data) {
|
public static <B> R<B> ok(B data) {
|
||||||
return new R().put("data", data);
|
return new R<B>(1000 , "ok", data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public R put(String key, Object value) {
|
|
||||||
super.put(key, value);
|
public R<T> put(String key, Object value) {
|
||||||
|
if ( key.equals( "code") ) {
|
||||||
|
this.code = (int)value;
|
||||||
|
} else if ( key.equals( "message") ) {
|
||||||
|
this.message = (String)value;
|
||||||
|
} else if ( key.equals( "data") ) {
|
||||||
|
this.data = (T) value;
|
||||||
|
} else {
|
||||||
|
dataMap.put(key, value);
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user