Spring Boot 中加入Swagger-UI

要想SpringBoot中加入Swagger,只需三个步骤:
1.在pom.xml文件的标签中添加一下内容:


io.springfox
springfox-swagger-ui
2.2.2


io.springfox
springfox-swagger2
2.2.2

2.与Application.class文件同等级下创建Swagger.class文件:
package com.ilex.jiutou;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger {

@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.ilex.jiutou"))
.paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("9私聊APIs")
.description("了解更多东西可以访问:http://www.jiutouxiang.com")
.termsOfServiceUrl("http://www.jiutouxiang.com")
.contact("程序猿:赵理想(2016.8.25")
.version("1.0)")
.build();
}

}

3.在相应API接口中编辑,例如:

@Controller
@Api(value="/about-us2",description="关于我们页面")
public class AboutUsController extends BaseController {

@RequestMapping(value = "/about-us", method = RequestMethod.GET)
@ApiOperation(value = "返回名字", notes = "参数描述")
public String test(Model model)
{
sideData(model);
return "aboutUs/index";
}

@RequestMapping(value = "/about-us2", method = RequestMethod.GET)
@ApiImplicitParam(name = "name", value = "用户姓名", required = true, dataType = "String")
@ResponseBody
@ApiOperation(value = "测试接口2", notes = "描述")
public String test3(  @PathVariable String name)
{
return name;
}

}

你可能感兴趣的:(Spring Boot 中加入Swagger-UI)