io.springfox
springfox-swagger2
2.6.1
io.springfox
springfox-swagger-ui
2.6.1
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;
import springfox.documentation.service.Tag;
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.bocom.gzxf.jcsj.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("实战指挥平台--基础数据API说明文档")
.description("2017.11.9上线版本")
//.termsOfServiceUrl("http://mindao.com.cn")
.contact("智慧消防研发部")
.version("1.0")
.build();
}
}
/**
* Controller
*
* @since 2017年9月8日上午9:39:32
* @author shanming.yang
*
*/
@RestController
@RequestMapping("/user")
@Validated
@Api(value = "USER", description = "测试UserController")
public class UserController {
private static Logger logger = LogManager.getLogger(UserController.class.getName());
@Resource
private DemoService demoService;
@RequestMapping(value="",method={RequestMethod.GET })
@ApiOperation(value = "查询个人信息接口",notes = "查询个人信息接口")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "header", dataType="string", name = "token", value = "访问凭证", required = true),
})
public List query(@RequestParam("page") int page,
@RequestParam("count") int count) {
return demoService.query(page, count);
}
@RequestMapping(value="",method={RequestMethod.POST })
@ApiOperation(value = "增加个人信息接口",notes = "增加个人信息接口")
public void insert(@RequestAttribute("name") String name) {
Demo model = new Demo();
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
model.setId(uuid);
model.setAge("25");
model.setName(name);
demoService.insert(model);
}
}
经过这3步配置后,我们启动服务后,访问:http://localhost:8080/swagger-ui.html 就完成了集成。
Swagger2默认将所有的Controller中的RequestMapping方法都会暴露,然而在实际开发中,我们并不一定需要把所有API都提现在文档中查看,这种情况下,使用注解@ApiIgnore来解决,如果应用在Controller范围上,则当前Controller中的所有方法都会被忽略,如果应用在方法上,则对应用的方法忽略暴露API。4、相关注解解读
附:使用Swagger生成JAVA Mock Server(Springboot)代码
http://blog.csdn.net/a78270528/article/details/78530702