springmvc 配置Swagger2
时间: 2023-06-02 20:07:58 浏览: 138
可以的,您可以参考以下配置方法:
1. 在 pom.xml 文件中引入 Swagger2 依赖:
```
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
```
2. 添加 Swagger2 配置类:
```
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SpringBoot APIs")
.description("This is a demo project for SpringBoot APIs.")
.version("1.0.0")
.build();
}
}
```
3. 在 Controller 中添加 Swagger2 注解:
```
@RestController
@RequestMapping("/api")
@Api(tags = "API接口")
public class SampleController {
@ApiOperation(value = "获取用户信息", notes = "根据用户id获取用户信息")
@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id) {
return userService.getUserById(id);
}
}
```
以上是基本的配置和注解方法,您可以根据实际需求进行更多的自定义配置和注解。
阅读全文
相关推荐

















