spring boot 整合swagger ui
程序员文章站
2022-05-04 16:26:53
...
spring boot 整合swagger ui
1.导入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
2.配置swagger工具类(界面显示,不配置也可以)
@Configuration
public class Swagger2Utils {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.example.test_kj.controller")) //扫描的controller地址
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder().title("springboot整合swagger")
.description("雨落") //描述
.termsOfServiceUrl("https://blog.csdn.net/qq_35414330/article/details/82422697") //链接
.version("1.0").build();
}
}
3.controller
@Controller
@Api("用户信息")
@RequestMapping("user")
public class UserController {
@Autowired
private TestInterFace testInterFace;
@RequestMapping(value = "/getUserById/{id}",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
// @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
public User getUser(@PathVariable(value = "id") String userId) {
return testInterFace.testUser(userId);
}
@RequestMapping(value = "/getUserList",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "获取用户列表",notes = "获取用户列表")
public List<User> getUserList(){
return testInterFace.getUserList();
}
@RequestMapping(value = "/add",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value="创建用户", notes="根据User对象创建用户")
// @ApiImplicitParam(name = "reqData", value = "用户详细实体user", required = true, dataType = "AddUserRequest")
public String add(@RequestBody AddUserRequest reqData) {
testInterFace.insertUser(reqData);
return "插入成功";
}
@ApiOperation(value="删除用户", notes="根据url的id来指定删除用户")
// @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
@RequestMapping(value = "deleteById/{id}",method = RequestMethod.GET)
@ResponseBody
public String deleteById(@PathVariable(value = "id")String id){
testInterFace.deleteById(id);
return "删除成功";
}
}
4.开启swagger2 (@EnableSwagger2)
@SpringBootApplication
@EnableSwagger2 //开启swagger2
public class TestKjApplication {
public static void main(String[] args) {
SpringApplication.run(TestKjApplication.class, args);
}
}
5.启动项目,访问 http://localhost:8080/swagger-ui.html
推荐阅读
-
Spring Boot 项目中使用Swagger2的示例
-
Spring Boot+Mybatis的整合过程
-
详解spring cloud整合Swagger2构建RESTful服务的APIs
-
Spring Boot + Kotlin整合MyBatis的方法教程
-
Spring Boot集成Swagger2项目实战
-
spring boot整合Shiro实现单点登录的示例代码
-
Spring Boot整合ElasticSearch实现多版本兼容的方法详解
-
Spring Boot整合mybatis并自动生成mapper和实体实例解析
-
spring boot整合quartz实现多个定时任务的方法
-
spring boot整合CAS Client实现单点登陆验证的示例