1.枚举类,列出所需要的状态码
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public enum StatusCodeEnum {
STATUS200(200,"操作成功"),
STATUS201(201,"操作失败"),
STATUS403(403,"无权访问"),
STATUS404(404,"请求资源不存在"),
STATUS500(500,"系统异常");
private final Integer code;
private final String msg;
}
2.编写统一返回结果工具类
import lombok.Data;
import java.io.Serializable;
@Data
public class Result<T> implements Serializable {
private Integer code;
private String msg;
private T data;
public static <T>Result<T>success(T data) {
return Result.success(StatusCodeEnum.STATUS200.getMsg(),data);
}
public static <T> Result<T> success(String msg,T data) {
Result<T> result = new Result<>();
result.setCode(StatusCodeEnum.STATUS200.getCode());
result.setMsg(msg);
result.setData(data);
return result;
}
public static Result error(Integer code, String msg) {
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
}
3.全局异常错误处理
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class GlobalExcrptionHandler {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public Result<String> handle(Exception e){
log.error("全局异常信息:{}",e.getMessage());
return Result.error(StatusCodeEnum.STATUS500.getCode(), StatusCodeEnum.STATUS500.getMsg()+":"+e.getMessage());
}
}
4.编写一个实现ResponseBodyAdvice接口的类来实现接口调用的统一返回值。
import com.alibaba.fastjson.JSON;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
@ControllerAdvice
@ResponseBody
public class MyResponseAdvice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest request, ServerHttpResponse response) {
if(body instanceof String){
return JSON.toJSONString(Result.success(body));
}
if(body instanceof Result){
return body;
}
return Result.success(body);
}
}
5.具体应用
实体类
import lombok.Data;
@Data
public class Person {
private Integer id;
private String name;
private String sex;
}
mapper类
import com.example.demo.entity.Person;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface PersonMapper {
@Insert("insert into person (name,sex) values(#{name},#{sex})")
public Integer insert(Person person);
@Select("select * from person")
public Person select();
}
controller类
import com.example.demo.common.Result;
import com.example.demo.entity.Person;
import com.example.demo.mapper.PersonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PersonController {
@Autowired
PersonMapper personMapper;
@PostMapping("/personAdd")
public Integer insert(@RequestBody Person person){
return personMapper.insert(person);
}
@GetMapping("/selectPerson")
public Person select(){
return personMapper.select();
}
}
查询接口应用的返回值
{ "code": 200, "msg": "操作成功", "data": { "id": 1, "name": "张三", "sex": "女" } }
附:把所有导入的包写出来是为了方便知道所需要的依赖