- 创建枚举类 方便管理返回的 code 码
package com.lu.demo.util;
import lombok.Getter;
@Getter
public enum Enumeration {
SUCCESS(2000,"成功"),
ERROR(4000,"错误");
protected Integer code;
protected String message;
Enumeration(Integer code,String message){
this.code = code;
this.message =message;
}
}
- 创建统一响应类
package com.lu.demo.util;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
@Data
public class ResponseFormat {
private Integer code;
private String message;
private Map<String,Object> data = new HashMap<String,Object>();
private ResponseFormat(){}
public static ResponseFormat success(){
ResponseFormat responseFormat = new ResponseFormat();
responseFormat.setCode(Enumeration.SUCCESS.getCode());
responseFormat.setMessage(Enumeration.SUCCESS.getMessage());
return responseFormat;
}
public static ResponseFormat error(){
ResponseFormat responseFormat = new ResponseFormat();
responseFormat.setCode(Enumeration.ERROR.getCode());
responseFormat.setMessage(Enumeration.ERROR.getMessage());
return responseFormat;
}
public ResponseFormat code(Integer code){
this.setCode(code);
return this;
}
public ResponseFormat message(String message){
this.setMessage(message);
return this;
}
public ResponseFormat data(String key,Object value){
this.data.put(key,value);
return this;
}
public ResponseFormat data(Map<String,Object> map){
this.data = map;
return this;
}
}
- 使用示例
package com.lu.demo.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lu.demo.dto.UserDTO;
import com.lu.demo.entity.User;
import com.lu.demo.service.UserService;
import com.lu.demo.util.ResponseFormat;
import com.lu.demo.vo.user.AllListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@RestController
@RequestMapping("api/v1")
@Valid
public class UserController {
@Autowired
private UserService userService;
@GetMapping(value = {"/list/{name}","/list"})
public ResponseFormat list(@PathVariable(required = false) String name){
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByDesc(User::getCreateTime);
queryWrapper.like(!StringUtils.isEmpty(name),User::getUserName,name);
List<User> userList = userService.list(queryWrapper);
ArrayList<AllListVO> lists = new ArrayList<>();
userList.forEach(item->{
AllListVO vo = new AllListVO();
BeanUtils.copyProperties(item,vo);
vo.setTime(item.getCreateTime());
lists.add(vo);
});
HashMap<String, Object> map = new HashMap<>();
map.put("total",userService.count());
map.put("items",lists);
return ResponseFormat.success().data(map);
//或者 return ResponseFormat.success().data("items",lists);
}
@PostMapping("/create")
public ResponseFormat create(@RequestBody @Validated UserDTO userDTO){
User user = new User();
user.setEmail(userDTO.getEmail());
user.setUserName(userDTO.getName());
user.setAge(userDTO.getAge());
boolean result = userService.save(user);
return ResponseFormat.success();
}
}
ResponseFormat 类可以通过链式来调用方便快捷
例如:
1、ResponseFormat.success();
2、ResponseFormat.success().code(2000).message(“获取成功”);
3、ResponseFormat.success().data();