-
-
Notifications
You must be signed in to change notification settings - Fork 11k
/
Copy pathUniformReponseHandler.java
47 lines (36 loc) · 1.95 KB
/
UniformReponseHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.example.awaysuse.callmsg;
import com.example.awaysuse.result.ResultCode;
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.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice(annotations = RestController.class)
public class UniformReponseHandler<T> {
@ResponseStatus(HttpStatus.OK)
public CallResultMsg sendSuccessResponse(){
return new CallResultMsg(true, CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMsg(), null);
}
@ResponseStatus(HttpStatus.OK)
public CallResultMsg sendSuccessResponse(T data) {
return new CallResultMsg(true, CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMsg(), data);
}
@ExceptionHandler(UserDefinedException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public CallResultMsg sendErrorResponse_UserDefined(Exception exception){
// return new CallResultMsg(false, ((UserDefinedException)exception).getException().getCode(), ((UserDefinedException)exception).getException().getMsg(), null);
return new CallResultMsg(false, HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), null);
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.BAD_GATEWAY)
public CallResultMsg badGatewayResponse(Exception exception){
if (exception instanceof UserDefinedException) {
return this.sendErrorResponse_UserDefined(exception);
}
return new CallResultMsg(false, CodeAndMsg.BAD_GATEWAY.getCode(),CodeAndMsg.BAD_GATEWAY.getMsg(),null);
}
@ResponseStatus(HttpStatus.NOT_FOUND)
public CallResultMsg notFoundErrorResponse_System(){
return new CallResultMsg(false, CodeAndMsg.NOT_FOUND.getCode(),CodeAndMsg.NOT_FOUND.getMsg(),null);
}
}