Swagger2+swagger-bootstrap-ui的使用

1.首先引入所需的Pom文件


    io.springfox
    springfox-spring-web
    2.9.2


    io.springfox
    springfox-swagger2
    2.9.2


    com.github.xiaoymin
    swagger-bootstrap-ui
    1.9.6

 2.编写配置文件

@Configuration
@EnableSwagger2
public class SwaggerConfiguration{

    /**
     * 配置Swagger信息
     */
    @Bean
    public Docket createRestApi() {
        /*==========添加head参数start================*/
        ParameterBuilder tokenPar = new ParameterBuilder();
        List pars = new ArrayList();
        tokenPar.name("token").description("Token令牌").modelRef(new  ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.xx.xx.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);//需放置build之后
        ;
        return docket;
    }
    private ApiInfo apiInfo(){
        //作者信息
        return new ApiInfoBuilder()
                // 页面标题
                .title("RESTful APIs")
                .description("API接口文档")
                .termsOfServiceUrl("http://localhost:8089/")
                .contact(new Contact("作者","网址","邮箱"))
                .version("v1.0")
                .build();
    }
}

3.Controller类的使用

@Api(value="用户模块",tags={"用户操作接口"})
@RestController
@RequestMapping("/api/user")
public class UserController{

    @Autowired
    private UserService userService;

    /**
     * 查询所有用户
     * @return
     */
    @PostMapping("findAll")
    @ApiOperation(value = "获取所有用户", notes = "务必提交json格式")
    public List findAll(){
        return userService.findAll();
    }
​
    @PostMapping(value = "login")
    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", required = true,     
                              paramType = "form"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, 
                              paramType = "form")
    })
    public User login(@ApiIgnore LoginDo loginDo) {
        return userService.login(loginDo);
    }
}

​

}

你可能感兴趣的:(Swagger2)