java swagger 引入和配置

在Java项目中集成Swagger用于生成API文档,你需要按照以下步骤进行:

1. 引入依赖

首先,在你的pom.xml文件中添加Swagger的相关依赖。这里以Springfox Swagger 3.0.0为例(请根据实际需要选择合适版本):

 

xml

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

如果你需要使用旧版Swagger UI,可以添加以下依赖:

 

xml

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

2. 配置Swagger

创建一个配置类,通常命名为SwaggerConfig,并使用@EnableSwagger2WebMvc@EnableOpenApi(针对Springfox 3.0.0及以上版本)注解来启用Swagger配置。例如:

 

java

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.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                // 选择哪些控制器生成文档
                .apis(RequestHandlerSelectors.any())
                // 匹配所有路径
                .paths(PathSelectors.any())
                .build();
    }
}

3. 使用Swagger注解

在你的API控制器类上使用@Api注解,为每个API方法使用@ApiOperation,以及其他相关的注解如@ApiParam@ApiResponses等来提供详细的文档信息。例如:

 

java

import io.swagger.annotations.*;

@Api(value = "用户管理", description = "用户管理相关接口")
@RestController
@RequestMapping("/users")
public class UserController {

    @ApiOperation("获取用户信息")
    @GetMapping("/{id}")
    public User getUser(@ApiParam(value = "用户ID", required = true) @PathVariable Long id) {
        // ...
    }

    // 其他方法...
}

4. 静态资源映射

确保你的应用能够正确地映射Swagger UI的静态资源。在Spring Boot中,你可能需要在WebSecurityConfigurerAdapterconfigure(HttpSecurity http)方法中添加如下代码,以便在未登录时也能访问Swagger UI:

 

java

http.authorizeRequests()
    .antMatchers("/swagger-ui/**").permitAll()
    .and()
    // 其他安全配置...

5. 启动应用

完成上述配置后,启动你的应用。Swagger UI通常可以通过http://localhost:8080/swagger-ui/(替换为你的应用端口)来访问。

请注意,Swagger的配置可能会因版本不同或具体需求而有所变化,确保查阅官方文档或最新版本的指南以获取最准确的信息。

5

58139685@163.com

今天 11:22

java swagger 引入和配置

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Swagger2是一个用于生成API文档的工具,可以方便地展示Web服务的接口文档,下面是Swagger2在Java项目中的配置步骤: 1. 引入Swagger2依赖 在项目的pom.xml文件中添加以下依赖: ```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> ``` 2. 配置Swagger2 在Spring Boot项目的启动类或者@Configuration配置类中,添加以下配置: ```java @Configuration @EnableSwagger2 // 开启Swagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) // 扫描指定包下的Controller .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("XXX项目API文档") .description("XXX项目的API文档") .version("1.0") .build(); } } ``` 其中,Docket是Swagger2的配置类,apiInfo()方法用于配置API文档信息,createRestApi()方法用于指定扫描的Controller所在的包路径。 3. 添加API文档注解 在需要生成API文档的Controller方法上,添加相应的注解,例如: ```java @RestController @RequestMapping("/user") @Api(tags = "用户管理相关接口") public class UserController { @ApiOperation("获取用户列表") @GetMapping("/list") public List<User> list() { // ... } @ApiOperation("添加用户") @PostMapping("/add") public void add(@RequestBody User user) { // ... } } ``` 其中,@Api注解用于指定接口所属的标签,@ApiOperation注解用于指定接口的描述信息。 4. 查看API文档 在项目启动后,访问http://localhost:port/swagger-ui.html即可查看API文档。在文档页面中,可以查看接口的详细信息、请求参数、响应信息等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三希

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值