Springboot统一返回接口+统一异常处理+后端参数校验,java架构师和算法工程师

*/

private String msg;

/**

  • 响应的具体数据

*/

private T data;

public ResultVO(T data) {

this(ResultCode.SUCCESS, data);

} public ResultVO(ResultCode resultCode, T data) {

this.code = resultCode.getCode();

this.msg = resultCode.getMsg();

this.data = data;

}}

1.2 枚举状态码定义

===============================================================================

package com.suruomo.unified.result;

import lombok.Getter;/** * @author suruomo * @date 2020/8/7 16:10

  • @description: 响应码枚举 */@Getterpublic enum ResultCode { //1000系列通用错误

SUCCESS(1000, “操作成功”),

FAILED(1001, “接口错误”),

VALIDATE_FAILED(1002, “参数校验失败”),

ERROR(1003, “未知错误”),

//2000系列用户错误

USER_NOT_EXIST(2000,“用户不存在”),

USER_LOGIN_FAIL(2001,“用户名或密码错误”),

USER_NOT_LOGIN(2002,“用户还未登录,请先登录”),

NO_PERMISSION(2003,“权限不足,请联系管理员”);

private int code; private String msg; ResultCode(int code, String msg) { this.code = code; this.msg = msg; }}

2.实体类+参数校验

==============================================================================

package com.suruomo.unified.pojo;

import lombok.Getter;

import lombok.NoArgsConstructor;

import lombok.Setter;

import javax.validation.constraints.Email;

import javax.validation.constraints.NotNull;

import javax.validation.constraints.Size;

/**

  • @author suruomo

  • @date 2020/8/7 15:41

  • @description: 实体类

*/

@Getter

@Setter

@NoArgsConstructor

public class User {

@NotNull(message = “用户id不能为空”)

private Long id; @NotNull(message = “用户账号不能为空”)

@Size(min = 6, max = 11, message = “账号长度必须是6-11个字符”)

private String account; @NotNull(message = “用户密码不能为空”)

@Size(min = 6, max = 11, message = “密码长度必须是6-16个字符”)

private String password; @NotNull(message = “用户邮箱不能为空”)

@Email(message = “邮箱格式不正确”)

private String email;}

3.全局异常处理

============================================================================

3.1 自定义异常APIException

=========================================================================================

package com.suruomo.unified.exception;

import com.suruomo.unified.result.ResultCode;

import lombok.Getter;

/**

  • @author suruomo

  • @date 2020/8/7 16:43

  • @description: 自定义异常

*/

@Getter

public class APIException extends RuntimeException {

private int code;

private String msg;

public APIException() {

this(ResultCode.FAILED);

} public APIException(ResultCode failed) {

this.code=failed.getCode();

this.msg=failed.getMsg();

}}

2.全局异常统一处理

==============================================================================

package com.suruomo.unified.exception;

import com.suruomo.unified.result.ResultCode;

import com.suruomo.unified.result.ResultVO;

import lombok.extern.slf4j.Slf4j;

import org.springframework.http.HttpStatus;

import org.springframework.validation.ObjectError;

import org.springframework.web.bind.MethodArgumentNotValidException;

import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.ResponseStatus;

import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.ArrayList;

import java.util.List;

/**

  • @author suruomo

  • @date 2020/8/7 15:39

  • @description: 全局异常处理类

*/

@RestControllerAdvice

@Slf4j

public class GlobalExceptionHandler {

/**

  • 自定义异常APIException

*/

@ExceptionHandler(APIException.class)

@ResponseStatus(HttpStatus.BAD_REQUEST)

public ResultVO APIExceptionHandler(APIException e) {

log.error(“api异常”);

return new ResultVO<>(ResultCode.FAILED, e.getMsg());

} /**

  • 方法参数错误异常

  • @param e

  • @return

*/

@ExceptionHandler(MethodArgumentNotValidException.class)

@ResponseStatus(HttpStatus.BAD_REQUEST)

public ResultVO MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {

log.error(“方法参数错误异常”);

List list=new ArrayList<>(); // 从异常对象中拿到ObjectError对象

if (!e.getBindingResult().getAllErrors().isEmpty()){

for(ObjectError error:e.getBindingResult().getAllErrors()){

list.add(error.getDefaultMessage().toString());

}

}

// 然后提取错误提示信息进行返回

return new ResultVO<>(ResultCode.VALIDATE_FAILED, list);

}

}

4.ResponseControllerAdvice-全局处理增强版Controller

================================================================================================================

  • 实现ResponseBodyAdvice接口

  • 重写supports和beforeBodyWrite方法

  • 避免Controller里返回数据每次都要用响应体来包装

package com.suruomo.unified.controller;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.suruomo.unified.exception.APIException;

import com.suruomo.unified.result.ResultVO;

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.RestControllerAdvice;

import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**

  • @author suruomo

  • @date 2020/8/7 16:38

  • @description: 全局处理增强版Controller,避免Controller里返回数据每次都要用响应体来包装

*/

@RestControllerAdvice(basePackages = {“com.suruomo.unified.controller”}) // 注意哦,这里要加上需要扫描的包

public class ResponseControllerAdvice implements ResponseBodyAdvice {

@Override

public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> aClass) {

// 如果接口返回的类型本身就是ResultVO那就没有必要进行额外的操作,返回false

return !returnType.getGenericParameterType().equals(ResultVO.class);

}

@Override

public Object beforeBodyWrite(Object data, MethodParameter returnType, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {

// String类型不能直接包装,所以要进行些特别的处理

if (returnType.getGenericParameterType().equals(String.class)) {

ObjectMapper objectMapper = new ObjectMapper();

try {

// 将数据包装在ResultVO里后,再转换为json字符串响应给前端

return objectMapper.writeValueAsString(new ResultVO<>(data));

} catch (JsonProcessingException e) {

throw new APIException();

}

}

// 将原本的数据包装在ResultVO里

return new Resu

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

ltVO<>(data);

}

}

注意直接返回String数据不能直接包装,所以需要额外处理

5.TestController

====================================================================================

package com.suruomo.unified.controller;

import com.suruomo.unified.pojo.User;

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;

import javax.validation.Valid;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值