天天看点

MySQL - Spring Boot select and return null

问:MySQL查询结果为空时会不会报错?

@GetMapping("test")
public void test(String id) {
  User user = userService.getUserById(id);
  
  System.out.println("user: " + user);
}      

根据id查询得到user,当user不存在时,打印结果如下:

user: null      

当user存在时,打印结果如下:

user: User(id=3, ..., createdDate=2021-04-21 00:00:17.0, updatedDate=2021-04-21 00:00:17.0, is_deleted=false)      

结论:MySQL查询结果可以为空,并且不会报错。

问:MySQL查询结果为空,何时会报错?

@GetMapping("test")
public void test(String id) {
    User user = userService.getUserById(id);
    
    System.out.println("user: " + user);

    System.out.println("id: " + user.getId());
}      

当user存在时,打印结果如下:

user: User(id=3, ..., createdDate=2021-04-21 00:00:17.0, updatedDate=2021-04-21 00:00:17.0, is_deleted=false)

id: 3      

根据id查询得到user,当user不存在时,打印结果如下:

user: null      
MySQL - Spring Boot select and return null

使用Swagger测试时,直接报错如上。

结论:空对象无法获取属性值!

问:MySQL查询结果为空时,.java 抛出异常信息,如何显示?

@Override
public void validateUser(String id) {
  if (null == userMapper.getUserById(id)) {
    throw new GlobalException(MessageConstant.INVALID_USER);
  }
}      

.java直接抛出异常,这是内部抛出异常信息,页面显示如下:

MySQL - Spring Boot select and return null

对于用户而言,看不懂异常信息,会直接认为是系统报错!

问:发生异常时,封装异常信息实现方式一

*Controller.java

@GetMapping("test")
public GlobalException test() {
  return new GlobalException(MessageConstant.FAIL_FIND_USER);
}      

GlobalException.java

@Data
@NoArgsConstructor
public class GlobalException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    private String msg;
    private int code = 500;

    public GlobalException(String msg) {
        super(msg);
        this.msg = msg;
    }
}      

发生异常时,抛出信息如下:

{
  "cause": null,
  "stackTrace": [
    .... ,
    {
      "methodName": "run",
      "fileName": "Thread.java",
      "lineNumber": 748,
      "className": "java.lang.Thread",
      "nativeMethod": false
    }
  ],
  "msg": "Fail to find user",
  "code": 500,
  "localizedMessage": "Fail to find user",
  "message": "Fail to find user",
  "suppressed": []
}      

异常信息内容过多,大概有250行左右。

问:发生异常时,封装异常信息实现方式二

  • ResponseResult.java
@Data
public class ResponseResult<T> {
    private final Integer code;
    private final String message;
    private final T data;

    ...

    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>(ResultStatusType.SUCCESS, data);
    }

    public static <T> ResponseResult<T> failure(ResultStatusType resultStatusType, String message, T data) {
        return new ResponseResult<>(resultStatusType, message, data);
    }
}      
  • ResultStatusType.java
@Getter
public enum ResultStatusType {
    SUCCESS(0, "OK"),
    BAD_REQUEST(HttpStatus.BAD_REQUEST.value(), "Bad Request"),
    INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Internal Server Error"),
    ;

    private final Integer code;
    private final String message;

    ResultStatusType(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
}      
  • UserController.java
@GetMapping("test")
public ResponseEntity<ResponseResult<String>> test(String id) {
  User user = userService.getUserById(id);

  if (user != null) {
    return new ResponseEntity<>(ResponseResult.success(MessageConstant.SUCCESS), HttpStatus.OK);
  } else {
    return new ResponseEntity<>(ResponseResult.failure(ResultStatusType.INTERNAL_SERVER_ERROR, MessageConstant.FAIL_FIND_USER, null), HttpStatus.BAD_REQUEST);
  }
}      

对象为空时,返回结果如下:

MySQL - Spring Boot select and return null