Swagerプロジェクトの適用
14019 ワード
Swaggerの説明
使用状況
- Web Front, Android, IOS ... などすべて
長所
-実際の運用では、セキュリティを高めるためにjwtなどのトークンを使用してログインして使用します.
ステージ
結果
1. Maven - Pom.xmlにSwaggerを追加 <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
2.Swagger構成クラスの追加 @EnableSwagger2
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.build()
.pathMapping("/")
.apiInfo(metaData());
}
private ApiInfo metaData() {
Contact contact = new Contact("성원준", "https://velog.io/@donsco", "[email protected]");
return new ApiInfo("",
"Spring Framework CAD",
"1.0",
"Terms of Service: ...",
contact,
"Apache License Version 2.0",
"https://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<>());
}
}
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
@EnableSwagger2
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.build()
.pathMapping("/")
.apiInfo(metaData());
}
private ApiInfo metaData() {
Contact contact = new Contact("성원준", "https://velog.io/@donsco", "[email protected]");
return new ApiInfo("",
"Spring Framework CAD",
"1.0",
"Terms of Service: ...",
contact,
"Apache License Version 2.0",
"https://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<>());
}
}
3.コントローラとDTOに説明を追加
Controller
@Api(description = "고객 컨트롤러")
@RestController
@RequestMapping(CustomerController.BASE_URL)
public class CustomerController {
public static final String BASE_URL = "/api/v1/customers";
private final CustomerService customerService;
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
@ApiOperation(value = "간략 설명", notes = "상세 설명")
@GetMapping
@ResponseStatus(HttpStatus.OK)
public CustomerListDTO getListOfCustomers(){
return new CustomerListDTO(customerService.getAllCustomers());
}
...
@Api(description = "고객 컨트롤러")
@RestController
@RequestMapping(CustomerController.BASE_URL)
public class CustomerController {
public static final String BASE_URL = "/api/v1/customers";
private final CustomerService customerService;
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
@ApiOperation(value = "간략 설명", notes = "상세 설명")
@GetMapping
@ResponseStatus(HttpStatus.OK)
public CustomerListDTO getListOfCustomers(){
return new CustomerListDTO(customerService.getAllCustomers());
}
...
コントローラ上部に@Apiプロンプトと説明を追加し、説明を追加します.
メソッドでは@ApiOperation宣言を使用して詳細に説明できます.
descriptionは現在Depecifiedになっているのでtags方式をお勧めします.
Swagger @Api Description is Deprecated
Api annotation's description is deprecated
DTO
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CustomerDTO {
@ApiModelProperty(value = "first name은 ~~입니다.", required = true)
private String firstname;
@ApiModelProperty(required = true )
private String lastname;
@JsonProperty("customer_url")
private String customerUrl;
}
Reference
この問題について(Swagerプロジェクトの適用), 我々は、より多くの情報をここで見つけました https://velog.io/@donsco/Swagger-다시-공부-및-프로젝트-적용テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol