Make the development of API services faster and more efficient (Only for springboot)

1、If you want to use request uniform response

Introducing dependencies to your pom.xml

1
2
3
4
5
<dependency> 
<groupId>com.lihansir.platform</groupId>
<artifactId>lihansir-spring-boot-starter</artifactId>
<version>${latest version}</version>
</dependency>

click here to find the latest version

Alternate link

Just need two annotation

1
2
@EnableRestResult 
@UseRestResult

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@EnableRestResult  // Enable the uniform response
@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

@UseRestResult
@RestController
public class DemoController {}

@RestController
public class DemoController {

@UseRestResult
@PostMapping("create")
public CreateOrderResultVO create(@Validated CreateOrderDTO createOrderDTO) {
return ...
}

}

Then all interfaces will return with a uniform response, like this

1
2
3
4
5
6
7
8
9
{
"data": {} // can be any object,such as String、List、Number...
"errorCode": "success",
"errorMessage": "success",
"host": "127.0.0.1",
"showType": 4,
"success": true,
"traceId": "04f3a87c-0d15-4832-8573-6676ea96546e"
}

You can use @UseRestResult on type or method

  • If you use @UseRestResult on type, that means that all methods in the class use a uniform response

  • If you use **@UseRestResul****t** on method, that means just this method use a uniform response

  • If you use @UseRestResult on type, but you want one of these methods not to use a uniform response, you can use @IgnoreRestResult on this method to ignore the uniform response

2、Built in global exception handling

All exceptions will be caught and returned using a unified response

1
2
3
4
5
6
7
8
9
{
"success": false,
"data": null,
"errorCode": "errorCode",
"errorMessage": "errorMessage",
"showType": 4,
"traceId": "f07a25e9-f4aa-4f75-8eb7-41d6402d70e5",
"host": "127.0.0.1"
}

Two exceptions (BusinessException, ParamException) are built in for developers to use in business code

You can implement RestCode to extended custom exception code

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
public enum CommonCode implements RestCode {

FAILED("request_failed", "failed"),

PARAM_CHECK_ERROR("param_check_error", "Parameter verification failed"),

SERVLET_ERROR("bad_request", "Bad request"),

ERROR_URL("request_path_not_found", "Request path error"),

BUSINESS_EXECUTE_ERROR("business_execute_error", "Business logic processing exception"),

ILLEGAL_ARGUMENT_ERROR("illegal_argument_error", "Illegal argument error"),

PROGRAM_EXECUTION_EXCEPTION("program_execution_exception", "Program execution exception");

private final String errorCode;

private final String errorMessage;

CommonCode(String errorCode, String errorMessage) {
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}

@Override
public String getErrorCode() {
return this.errorCode;
}

@Override
public String getErrorMessage() {
return this.errorMessage;
}

}
1
throw new BusinessException(CommonCode.PARAM_CHECK_ERROR);

Finally, I invite you to pay attention to my personal website: https://www.lihansir.com