To achieve unified encapsulation and enable Swagger, you just need to import the following dependencies in your pom.xml
. The more usage can be found in the spring-boot-demo.
<dependency>
<groupId>io.github.unickcheng</groupId>
<artifactId>response-handler-starter</artifactId>
<version>0.3.0</version>
</dependency>
Response-Handler is a unified interface the response body format for Spring developers. You can use a single annotation to take care of exception catching, data return, and OpenAPI 3. If you are interested in this project, feel free to incubate it together, details can be found in How to contribute.
- Unified response body format
- Support automatic catching of common exceptions, reducing the need to manually catch exceptions after they are thrown
- Support for extended custom status codes
- Built-in support for OpenAPI 3, i.e. Swagger 3
- Available for download from the Maven central repository (OSSRH-87858)
- Support encryption of returned data (later consider using annotations to build in AES, RSA, SM and other encryption methods)
You need to add the following dependency to pom.xml and then run mvn install
to download this dependency, note that the version number may be out of date, you can check the latest version in the Maven central repository
<dependency>
<groupId>io.github.unickcheng</groupId>
<artifactId>response-handler-starter</artifactId>
<version>0.3.0</version>
</dependency>
If you want to use the latest development version, you can append the GitHub repository address to pom.xml. Note that using the latest development version may have a number of instabilities
<!--Add the GitHub repository address-->
<repositories>
<repository>
<id>unickcheng</id>
<url>https://unickcheng.github.io/maven-packages/</url>
</repository>
</repositories>
If you don't know how to get started, you can refer to or use spring-boot-demo to familiarize yourself with the process
Please visit http://<your-ip>:8080/openapi-ui.html
on the web side, if your port number is not the default 8080, please change it
The response body structure contains the request time, and the default time zone is Asia/Shanghai
. Since the field is formatted with @JsonFormat
, you can easily change it in the application
configuration file, note that you can only change the time zone at the moment
# @JsonFormat: set time zone
spring.jackson.time-zone=Asia/Shanghai
This part uses the factory design pattern, where you simply implement the ExceptionStatusInfo
interface to encapsulate a custom status code enumeration class, and then throw a custom exception in the appropriate method, see ReturnStatus.java
throw new RHandlerException(ReturnStatus.CUSTOM_ERROR);
If you don't need this for now, i recommend using org.springframework.http.HttpStatus
as a status code for quick development. In short, you don't need to add additional actions, just throw exceptions like the following code in the corresponding method
throw new RHandlerException(HttpStatus.BAD_REQUEST);
- Spring Boot provides framework support
- Open Source Dependencies provides support
- Thanks to all open source projects for sharing ideas and techniques