返回结果ResultInfoUtil

    1. import com.ck.syscheck.model.ResultInfo;
    2. /**
    3. * 返回结果工具类
    4. *
    5. * @author ck
    6. * @date 2019/7/1
    7. */
    8. public class ResultInfoUtil {
    9. public static ResultInfo success(Object object) {
    10. ResultInfo resultInfo = new ResultInfo();
    11. resultInfo.setData(object);
    12. resultInfo.setCode(0);
    13. resultInfo.setMsg("成功");
    14. return resultInfo;
    15. }
    16. public static ResultInfo success() {
    17. return success(null);
    18. }
    19. public static ResultInfo error(Integer code, String msg) {
    20. ResultInfo resultInfo = new ResultInfo();
    21. resultInfo.setCode(code);
    22. resultInfo.setMsg(msg);
    23. return resultInfo;
    24. }
    25. }

    ResultInfo实体类:

    1. import com.fasterxml.jackson.annotation.JsonInclude;
    2. import lombok.Data;
    3. import java.io.Serializable;
    4. /**
    5. * http请求返回的最外层对象
    6. *
    7. * @author ck
    8. * @date 2019/7/1
    9. */
    10. @Data
    11. //@JsonInclude(JsonInclude.Include.NON_NULL)
    12. public class ResultInfo<T> implements Serializable {
    13. private static final long serialVersionUID = 409897222541150194L;
    14. /**
    15. * 错误码
    16. */
    17. private Integer code;
    18. /**
    19. * 提示信息
    20. */
    21. private String msg;
    22. /**
    23. * 具体内容
    24. */
    25. private T data;
    26. }

    如何使用:

    1. //订单列表
    2. @GetMapping("/list")
    3. public ResultInfo<List<OrderDTO>> list(@RequestParam("openid") String openid,
    4. @RequestParam(value = "page",defaultValue = "0") Integer page,
    5. @RequestParam(value = "size", defaultValue = "10") Integer size){
    6. if(StringUtils.isEmpty(openid)){
    7. log.error("【查询订单列表】openid为空");
    8. throw new SellException(ResultEnum.PARAM_ERROR);
    9. }
    10. PageRequest request=new PageRequest(page,size);
    11. Page<OrderDTO> orderDTOPage=orderService.findList(openid,request);
    12. return ResultInfoUtil.success(orderDTOPage.getContent());
    13. }
    14. //订单详情
    15. @GetMapping("/detail")
    16. public ResultInfo<OrderDTO> detail(@RequestParam("openid") String openid,
    17. @RequestParam("orderId") String orderId){
    18. OrderDTO orderDTO=buyerService.findOrderOne(openid,orderId);
    19. return ResultInfoUtil.success(orderDTO);
    20. }
    21. //取消订单
    22. @PostMapping("/cancel")
    23. public ResultInfo cancel(@RequestParam("openid") String openid,
    24. @RequestParam("orderId") String orderId){
    25. buyerService.cancelOrder(openid,orderId);
    26. return ResultInfoUtil.success();
    27. }