在Linux系統中,Swagger的錯誤處理機制可以通過以下步驟進行設置:
首先,確保你的Swagger文檔(通常是swagger.yaml
或swagger.json
)中定義了錯誤響應。例如:
paths:
/users/{userId}:
get:
summary: Get user by ID
parameters:
- in: path
name: userId
required: true
type: string
responses:
'200':
description: Successful response
schema:
type: object
properties:
id:
type: string
name:
type: string
'404':
description: User not found
schema:
type: object
properties:
error:
type: string
'500':
description: Internal server error
schema:
type: object
properties:
error:
type: string
在Spring Boot應用中,你可以通過實現@ControllerAdvice
和@ExceptionHandler
注解來創建自定義錯誤處理器。
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(UserNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<ErrorResponse> handleUserNotFoundException(UserNotFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.NOT_FOUND.value(), ex.getMessage());
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) {
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "An unexpected error occurred");
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
public class ErrorResponse {
private int status;
private String message;
public ErrorResponse(int status, String message) {
this.status = status;
this.message = message;
}
// Getters and setters
}
在你的業務邏輯中,當遇到特定錯誤時,拋出自定義異常。
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String message) {
super(message);
}
}
確保你的Swagger UI配置正確,以便能夠顯示錯誤信息。
如果你使用的是Spring Boot,添加以下依賴到pom.xml
:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
創建一個Swagger配置類:
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo"))
.paths(PathSelectors.any())
.build();
}
}
啟動你的Spring Boot應用,并訪問Swagger UI(通常是http://localhost:8080/swagger-ui.html
)。你應該能夠看到定義的錯誤響應,并且在調用API時,如果發生錯誤,Swagger UI會顯示相應的錯誤信息。
通過以上步驟,你可以在Linux系統中設置Swagger的錯誤處理機制,確保API的錯誤響應能夠被正確地記錄和展示。