diff --git a/api-portal/src/main/java/com/jmsoftware/apiportal/universal/controller/CommonController.java b/api-portal/src/main/java/com/jmsoftware/apiportal/universal/controller/CommonController.java index 600f7d2a..42c74bac 100644 --- a/api-portal/src/main/java/com/jmsoftware/apiportal/universal/controller/CommonController.java +++ b/api-portal/src/main/java/com/jmsoftware/apiportal/universal/controller/CommonController.java @@ -29,14 +29,14 @@ public class CommonController { @ApiOperation(value = "/app-info", notes = "Retrieve application information") public ResponseBodyBean> applicationInformation() { var data = commonService.getApplicationInfo(); - return ResponseBodyBean.ofDataAndMessage(data, "Succeed to retrieve app info."); + return ResponseBodyBean.ofSuccess(data, "Succeed to retrieve app info."); } @PostMapping("/validation-test") @ApiOperation(value = "/validation-test", notes = "Validation of request payload test") public ResponseBodyBean validationTest(@RequestBody ValidationTestPayload payload) { commonService.validateObject(payload); - return ResponseBodyBean.ofDataAndMessage(payload.getName(), "validationTest()"); + return ResponseBodyBean.ofSuccess(payload.getName(), "validationTest()"); } @GetMapping("/get-jwt") diff --git a/auth-center/src/main/java/com/jmsoftware/authcenter/universal/controller/CommonController.java b/auth-center/src/main/java/com/jmsoftware/authcenter/universal/controller/CommonController.java index 8ed0a7e1..8c937e5e 100644 --- a/auth-center/src/main/java/com/jmsoftware/authcenter/universal/controller/CommonController.java +++ b/auth-center/src/main/java/com/jmsoftware/authcenter/universal/controller/CommonController.java @@ -32,13 +32,13 @@ public class CommonController { public ResponseBodyBean> applicationInformation() { var data = commonService.getApplicationInfo(); redisService.set("appInfo", data.toString()); - return ResponseBodyBean.ofDataAndMessage(data, "Succeed to retrieve app info."); + return ResponseBodyBean.ofSuccess(data, "Succeed to retrieve app info."); } @PostMapping("/validation-test") @ApiOperation(value = "/validation-test", notes = "Validation of request payload test") public ResponseBodyBean validationTest(@RequestBody ValidationTestPayload payload) { commonService.validateObject(payload); - return ResponseBodyBean.ofDataAndMessage(payload.getName(), "validationTest()"); + return ResponseBodyBean.ofSuccess(payload.getName(), "validationTest()"); } } diff --git a/common/src/main/java/com/jmsoftware/common/bean/ResponseBodyBean.java b/common/src/main/java/com/jmsoftware/common/bean/ResponseBodyBean.java index abb179f4..0e2476a4 100644 --- a/common/src/main/java/com/jmsoftware/common/bean/ResponseBodyBean.java +++ b/common/src/main/java/com/jmsoftware/common/bean/ResponseBodyBean.java @@ -7,6 +7,7 @@ import com.jmsoftware.common.exception.BusinessException; import lombok.Builder; import lombok.Getter; +import lombok.NonNull; import lombok.ToString; import java.io.Serializable; @@ -17,7 +18,7 @@ *

* Response body bean. * - * @param the body type + * @param the response body data type * @author Johnny Miller (鍾俊), e-mail: johnnysviva@outlook.com * @date 2/27/20 9:24 AM */ @@ -25,7 +26,7 @@ @Builder @ToString @SuppressWarnings("unused") -public class ResponseBodyBean implements Serializable { +public class ResponseBodyBean implements Serializable { /** * The constant serialVersionUID. */ @@ -35,19 +36,19 @@ public class ResponseBodyBean implements Serializable { * The Timestamp. */ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") - private Date timestamp; + private final Date timestamp; /** * Default status is OK[200] */ - private Integer status; + private final Integer status; /** * The Message. */ - private String message; + private final String message; /** * The Data. */ - private T data; + private final ResponseBodyDataType data; /** *

Respond to client with IUniversalStatus (status may be OK or other).

@@ -55,12 +56,13 @@ public class ResponseBodyBean implements Serializable { *

This method CANNOT be used by controller or service or other class, only provided for Exception controller * .

* - * @param the type parameter - * @param status IUniversalStatus + * @param the type parameter + * @param status IUniversalStatus * @return response body for ExceptionControllerAdvice javax.servlet.http.HttpServletResponse, Exception) */ - public static ResponseBodyBean ofStatus(final IUniversalStatus status) { - return ResponseBodyBean.builder().timestamp(new Date()) + public static ResponseBodyBean ofStatus(@NonNull final IUniversalStatus status) { + return ResponseBodyBean.builder() + .timestamp(new Date()) .status(status.getCode()) .message(status.getMessage()) .build(); @@ -72,14 +74,15 @@ public static ResponseBodyBean ofStatus(fin *

This method CANNOT be used by controller or service or other class, only provided for Exception controller * .

* - * @param the type parameter - * @param status IUniversalStatus - * @param data data to be responded to client + * @param the response body data type + * @param status IUniversalStatus + * @param data data to be responded to client * @return response body for ExceptionControllerAdvice */ - public static ResponseBodyBean ofStatus(final IUniversalStatus status, - final ResponseBodyType data) { - return ResponseBodyBean.builder().timestamp(new Date()) + public static ResponseBodyBean ofStatus(@NonNull final IUniversalStatus status, + @NonNull final ResponseBodyDataType data) { + return ResponseBodyBean.builder() + .timestamp(new Date()) .status(status.getCode()) .message(status.getMessage()) .data(data) @@ -92,16 +95,21 @@ public static ResponseBodyBean ofStatus(fin *

This method CANNOT be used by controller or service or other class, only provided for Exception controller * .

* - * @param the type parameter - * @param status status code - * @param message message to be responded - * @param data data to be responded + * @param the response body data type + * @param status status code + * @param message message to be responded + * @param data data to be responded * @return response body for ExceptionControllerAdvice */ - public static ResponseBodyBean ofStatus(final Integer status, - final String message, - final ResponseBodyType data) { - return ResponseBodyBean.builder().timestamp(new Date()).status(status).message(message).data(data).build(); + public static ResponseBodyBean ofStatus(@NonNull final Integer status, + @NonNull final String message, + @NonNull final ResponseBodyDataType data) { + return ResponseBodyBean.builder() + .timestamp(new Date()) + .status(status) + .message(message) + .data(data) + .build(); } /** @@ -109,31 +117,38 @@ public static ResponseBodyBean ofStatus(fin *

ATTENTION:

*

This method CANNOT be used in ExceptionControllerAdvice.

* - * @param the type parameter - * @param status status code - * @param message message to be responded - * @param data data to be responded + * @param the response body data type + * @param status status code + * @param message message to be responded + * @param data data to be responded * @return response body */ - public static ResponseBodyBean setResponse(final Integer status, - final String message, - final ResponseBodyType data) { + public static ResponseBodyBean setResponse(@NonNull final Integer status, + @NonNull final String message, + final ResponseBodyDataType data) { if (!HttpStatus.OK.getCode().equals(status)) { throw new BaseException(status, message, data); } - return ResponseBodyBean.builder().timestamp(new Date()).status(status).message(message).data(data).build(); + return ResponseBodyBean.builder() + .timestamp(new Date()) + .status(status) + .message(message) + .data(data) + .build(); } /** * Respond data and status is OK. * - * @param the type parameter - * @param data data to be responded to client. + * @param the response body data type + * @param data data to be responded to client. * @return response body */ - public static ResponseBodyBean ofData(final ResponseBodyType data) { - return ResponseBodyBean.builder().timestamp(new Date()) + public static ResponseBodyBean ofData(@NonNull final ResponseBodyDataType data) { + return ResponseBodyBean.builder() + .timestamp(new Date()) .status(HttpStatus.OK.getCode()) + .message(HttpStatus.OK.getMessage()) .data(data) .build(); } @@ -141,57 +156,41 @@ public static ResponseBodyBean ofData(final /** * Respond a message and status id OK. * - * @param the type parameter - * @param message message to be responded - * @return response body - */ - public static ResponseBodyBean ofMessage(final String message) { - return ResponseBodyBean.builder().timestamp(new Date()) - .status(HttpStatus.OK.getCode()) - .message(message) - .build(); - } - - /** - * Respond data, message and status is OK. - * - * @param the type parameter - * @param data data to be responded - * @param message message to be responded + * @param the response body data type + * @param message message to be responded * @return response body */ - public static ResponseBodyBean ofDataAndMessage(final ResponseBodyType data, - final String message) { - return ResponseBodyBean.builder().timestamp(new Date()) + public static ResponseBodyBean ofMessage(@NonNull final String message) { + return ResponseBodyBean.builder() + .timestamp(new Date()) .status(HttpStatus.OK.getCode()) - .data(data) .message(message) .build(); } /** - * Respond data, and status is OK. + * Respond null data, and status is OK. * - * @param the type parameter - * @param data data to be responded + * @param the response body data type * @return response body */ - public static ResponseBodyBean ofSuccess(final ResponseBodyType data) { - return ResponseBodyBean.builder().timestamp(new Date()) + public static ResponseBodyBean ofSuccess() { + return ResponseBodyBean.builder() + .timestamp(new Date()) .status(HttpStatus.OK.getCode()) - .data(data) + .message(HttpStatus.OK.getMessage()) .build(); } /** * Respond a message and status is OK. * - * @param the type parameter - * @param message message to be responded + * @param the response body data type + * @param message message to be responded * @return response body */ - public static ResponseBodyBean ofSuccess(final String message) { - return ResponseBodyBean.builder().timestamp(new Date()) + public static ResponseBodyBean ofSuccess(@NonNull final String message) { + return ResponseBodyBean.builder().timestamp(new Date()) .status(HttpStatus.OK.getCode()) .message(message) .build(); @@ -200,84 +199,85 @@ public static ResponseBodyBean ofSuccess(fi /** * Respond data, message and status is OK. * - * @param the type parameter - * @param data data to be responded - * @param message message to be responded + * @param the response body data type + * @param data data to be responded + * @param message message to be responded * @return response body */ - public static ResponseBodyBean ofSuccess(final ResponseBodyType data, - final String message) { - return ResponseBodyBean.builder().timestamp(new Date()) + public static ResponseBodyBean ofSuccess(@NonNull final ResponseBodyDataType data, + @NonNull final String message) { + return ResponseBodyBean.builder().timestamp(new Date()) .status(HttpStatus.OK.getCode()) - .data(data) .message(message) + .data(data) .build(); } /** * Respond a message and status is FAILURE(464). * - * @param the type parameter - * @param message message to be responded. + * @param the response body data type + * @param message message to be responded. * @return response body */ - public static ResponseBodyBean ofFailure(final String message) { + public static ResponseBodyBean ofFailure(@NonNull final String message) { throw new BusinessException(message); } /** * Respond a message and status is FAILURE(464). * - * @param the type parameter - * @param data data to be responded + * @param the response body data type + * @param data data to be responded * @return response body */ - public static ResponseBodyBean ofFailure(final Object data) { + public static ResponseBodyBean ofFailure(@NonNull final Object data) { throw new BusinessException(data); } /** * Respond data and message, and status if FAILURE(464). * - * @param the type parameter - * @param data data to be responded - * @param message message to be responded + * @param the response body data type + * @param data data to be responded + * @param message message to be responded * @return response body */ - public static ResponseBodyBean ofFailure(final ResponseBodyType data, - final String message) { + public static ResponseBodyBean ofFailure(@NonNull final ResponseBodyDataType data, + @NonNull final String message) { throw new BusinessException(data, message); } /** * Respond an ERROR(500). * - * @param the type parameter + * @param the response body data type * @return response body */ - public static ResponseBodyBean ofError() { + public static ResponseBodyBean ofError() { return setResponse(HttpStatus.ERROR.getCode(), HttpStatus.ERROR.getMessage(), null); } /** * Respond a custom error. * - * @param the type parameter - * @param status Error status, not OK(200) + * @param the response body data type + * @param status Error status, not OK(200) * @return response body */ - public static ResponseBodyBean ofError(final IUniversalStatus status) { + public static ResponseBodyBean ofError(@NonNull final IUniversalStatus status) { return setResponse(status.getCode(), status.getMessage(), null); } /** * Response an exception. * - * @param Sub class of {@link BaseException} - * @param throwable exception - * @return Exception information + * @param the response body data type + * @param Sub class of {@link BaseException} + * @param throwable exception + * @return the response body bean */ - public static ResponseBodyBean ofException(final BaseThrowable throwable) { - throw new BaseException(throwable.getCode(), throwable.getMessage(), throwable.getData()); + public static ResponseBodyBean ofException(@NonNull final BaseThrowable throwable) { + throw throwable; } } diff --git a/exercise-mis/src/main/java/com/jmsoftware/exercisemis/universal/controller/CommonController.java b/exercise-mis/src/main/java/com/jmsoftware/exercisemis/universal/controller/CommonController.java index 3fb3741c..203a100b 100644 --- a/exercise-mis/src/main/java/com/jmsoftware/exercisemis/universal/controller/CommonController.java +++ b/exercise-mis/src/main/java/com/jmsoftware/exercisemis/universal/controller/CommonController.java @@ -29,13 +29,13 @@ public class CommonController { @ApiOperation(value = "/app-info", notes = "Retrieve application information") public ResponseBodyBean> applicationInformation() { var data = commonService.getApplicationInfo(); - return ResponseBodyBean.ofDataAndMessage(data, "Succeed to retrieve app info."); + return ResponseBodyBean.ofSuccess(data, "Succeed to retrieve app info."); } @PostMapping("/validation-test") @ApiOperation(value = "/validation-test", notes = "Validation of request payload test") public ResponseBodyBean validationTest(@RequestBody ValidationTestPayload payload) { commonService.validateObject(payload); - return ResponseBodyBean.ofDataAndMessage(payload.getName(), "validationTest()"); + return ResponseBodyBean.ofSuccess(payload.getName(), "validationTest()"); } } diff --git a/gateway/src/main/java/com/jmsoftware/gateway/universal/controller/CommonController.java b/gateway/src/main/java/com/jmsoftware/gateway/universal/controller/CommonController.java index 16e7e918..7dd59dba 100644 --- a/gateway/src/main/java/com/jmsoftware/gateway/universal/controller/CommonController.java +++ b/gateway/src/main/java/com/jmsoftware/gateway/universal/controller/CommonController.java @@ -29,13 +29,13 @@ public class CommonController { @ApiOperation(value = "/app-info", notes = "Retrieve application information") public ResponseBodyBean> applicationInformation() { var data = commonService.getApplicationInfo(); - return ResponseBodyBean.ofDataAndMessage(data, "Succeed to retrieve app info."); + return ResponseBodyBean.ofSuccess(data, "Succeed to retrieve app info."); } @PostMapping("/validation-test") @ApiOperation(value = "/validation-test", notes = "Validation of request payload test") public ResponseBodyBean validationTest(@RequestBody ValidationTestPayload payload) { commonService.validateObject(payload); - return ResponseBodyBean.ofDataAndMessage(payload.getName(), "validationTest()"); + return ResponseBodyBean.ofSuccess(payload.getName(), "validationTest()"); } } diff --git a/service-registry/src/main/java/com/jmsoftware/serviceregistry/universal/controller/CommonController.java b/service-registry/src/main/java/com/jmsoftware/serviceregistry/universal/controller/CommonController.java index 7ea5a79f..b6482fc4 100644 --- a/service-registry/src/main/java/com/jmsoftware/serviceregistry/universal/controller/CommonController.java +++ b/service-registry/src/main/java/com/jmsoftware/serviceregistry/universal/controller/CommonController.java @@ -29,13 +29,13 @@ public class CommonController { @ApiOperation(value = "/app-info", notes = "Retrieve application information") public ResponseBodyBean> applicationInformation() { var data = commonService.getApplicationInfo(); - return ResponseBodyBean.ofDataAndMessage(data, "Succeed to retrieve app info."); + return ResponseBodyBean.ofSuccess(data, "Succeed to retrieve app info."); } @PostMapping("/validation-test") @ApiOperation(value = "/validation-test", notes = "Validation of request payload test") public ResponseBodyBean validationTest(@RequestBody ValidationTestPayload payload) { commonService.validateObject(payload); - return ResponseBodyBean.ofDataAndMessage(payload.getName(), "validationTest()"); + return ResponseBodyBean.ofSuccess(payload.getName(), "validationTest()"); } } diff --git a/spring-boot-admin/src/main/java/com/jmsoftware/springbootadmin/universal/controller/CommonController.java b/spring-boot-admin/src/main/java/com/jmsoftware/springbootadmin/universal/controller/CommonController.java index 45d413b3..a00501e5 100644 --- a/spring-boot-admin/src/main/java/com/jmsoftware/springbootadmin/universal/controller/CommonController.java +++ b/spring-boot-admin/src/main/java/com/jmsoftware/springbootadmin/universal/controller/CommonController.java @@ -29,13 +29,13 @@ public class CommonController { @ApiOperation(value = "/app-info", notes = "Retrieve application information") public ResponseBodyBean> applicationInformation() { var data = commonService.getApplicationInfo(); - return ResponseBodyBean.ofDataAndMessage(data, "Succeed to retrieve app info."); + return ResponseBodyBean.ofSuccess(data, "Succeed to retrieve app info."); } @PostMapping("/validation-test") @ApiOperation(value = "/validation-test", notes = "Validation of request payload test") public ResponseBodyBean validationTest(@RequestBody ValidationTestPayload payload) { commonService.validateObject(payload); - return ResponseBodyBean.ofDataAndMessage(payload.getName(), "validationTest()"); + return ResponseBodyBean.ofSuccess(payload.getName(), "validationTest()"); } }