返回结果ResultInfoUtil:
import com.ck.syscheck.model.ResultInfo;
/**
* 返回结果工具类
*
* @author ck
* @date 2019/7/1
*/
public class ResultInfoUtil {
public static ResultInfo success(Object object) {
ResultInfo resultInfo = new ResultInfo();
resultInfo.setData(object);
resultInfo.setCode(0);
resultInfo.setMsg("成功");
return resultInfo;
}
public static ResultInfo success() {
return success(null);
}
public static ResultInfo error(Integer code, String msg) {
ResultInfo resultInfo = new ResultInfo();
resultInfo.setCode(code);
resultInfo.setMsg(msg);
return resultInfo;
}
}
ResultInfo实体类:
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.io.Serializable;
/**
* http请求返回的最外层对象
*
* @author ck
* @date 2019/7/1
*/
@Data
//@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultInfo<T> implements Serializable {
private static final long serialVersionUID = 409897222541150194L;
/**
* 错误码
*/
private Integer code;
/**
* 提示信息
*/
private String msg;
/**
* 具体内容
*/
private T data;
}
如何使用:
//订单列表
@GetMapping("/list")
public ResultInfo<List<OrderDTO>> list(@RequestParam("openid") String openid,
@RequestParam(value = "page",defaultValue = "0") Integer page,
@RequestParam(value = "size", defaultValue = "10") Integer size){
if(StringUtils.isEmpty(openid)){
log.error("【查询订单列表】openid为空");
throw new SellException(ResultEnum.PARAM_ERROR);
}
PageRequest request=new PageRequest(page,size);
Page<OrderDTO> orderDTOPage=orderService.findList(openid,request);
return ResultInfoUtil.success(orderDTOPage.getContent());
}
//订单详情
@GetMapping("/detail")
public ResultInfo<OrderDTO> detail(@RequestParam("openid") String openid,
@RequestParam("orderId") String orderId){
OrderDTO orderDTO=buyerService.findOrderOne(openid,orderId);
return ResultInfoUtil.success(orderDTO);
}
//取消订单
@PostMapping("/cancel")
public ResultInfo cancel(@RequestParam("openid") String openid,
@RequestParam("orderId") String orderId){
buyerService.cancelOrder(openid,orderId);
return ResultInfoUtil.success();
}