Skip to content

UNICKCHENG/Response-Handler

Repository files navigation

Welcome to Response-Handler 👋

Update 2023-08-29

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>

publish

中文版

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.

✨ Features

  • 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)

🎉 Usage

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

😎 Extra play

1. web-side view of all interfaces

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

Pasted image 20230114213227.png

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

2. Custom the response body status code information

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);

✍️ ChangeLog

👍 Other interesting projects

💖 Credits

About

A maven dependency that handles response body, catches exceptions, supports for openapi

Topics

Resources

License

Stars

Watchers

Forks

Languages