ubuntu

Ubuntu Swagger支持哪些API规范

小樊
44
2025-04-03 07:01:03
栏目: 智能运维

Swagger在Java项目中的集成通常分为以下几个步骤:

  1. 添加依赖:在Spring Boot项目中,可以通过添加springfox-swagger2springfox-swagger-ui的依赖来实现Swagger的基本功能。
<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>
  1. 配置Swagger:在application.ymlapplication.properties中配置Swagger的相关信息。
springfox:
  swagger:
    base-path: /api
    group-name: "example"
    info:
      version: "1.0"
      title: "Example API"
  1. 注解API:在控制器类中使用@Api@ApiOperation@ApiParam等注解来定义API的描述、参数和返回值。
@RestController
@RequestMapping("/api")
public class ExampleController {
    @GetMapping("/hello")
    @ApiOperation(value = "返回示例信息", response = String.class)
    public String hello(@ApiParam(value = "用户ID", required = true) @RequestParam Long userId) {
        return "Hello, " + userId;
    }
}
  1. 启用Swagger UI:在主类中启用Swagger配置。
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example"))
                .paths(PathSelectors.any())
                .build();
    }
}

0
看了该问题的人还看了