spring boot 使用swagger-ui

依賴

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

設置

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.my.controller"))
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("項目標題")
                .description("項目描述")
                .contact(new Contact("bmyName", "localhost", ""))//作者信息
                .version("1.0")
                .build();

    }
}

使用

@Api(tags = "用戶管理相關")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;

    @ApiOperation("獲取所有用戶")
    @GetMapping("/getAllUser")
    public ApiFinalResult getAllUser() {
        return ApiFinalResult.success(userService.getAllUser());
    }

    @ApiOperation("添加用戶")
    @PostMapping("/addUser")
    public ApiFinalResult getAllUser(@Validated @RequestBody User user) {
        return ApiFinalResult.success(userService.addUser(user) == 1);
    }
}

測試

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章