欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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 整合swagger ui