1、创建Springboot项目
选择Spring Initializr,然后确认默认的SDK和url,如果url不对,在Custom里面填http://start.spring.io
确认后下一步
填写group、Artifact,type选择Gradle Project。
然后确认下一步创建项目
2、swagger配置。
编辑build.gradle,引用swagger依赖包
compile ‘io.springfox:springfox-swagger2:2.9.2’
compile ‘io.springfox:springfox-swagger-ui:2.9.2’
compile ‘io.swagger:swagger-jersey2-jaxrs:1.5.8’
compile(‘com.mangofactory:swagger-springmvc:1.0.2’)
compile(‘com.mangofactory:swagger-models:1.0.2’)
compile(‘com.wordnik:swagger-annotations:1.3.11’)
然后根据提示启动自动sync来自动build或者使用gradle工具条来手动build。
然后在主方法类启用@EnableSwagger2
然后新建swagger配置类,代码如下
package com.example.demo;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
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 java.util.HashSet;
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket documentation(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage(“com.example.demo”))
.build()
.protocols(new HashSet(Lists.newArrayList(“http”)))
.pathMapping("/")
.apiInfo(apiInfo());
}
/*@Bean
public UiConfiguration uiConfig(){
return UiConfiguration.DEFAULT;
}*/
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("服务API")
.description("服务端后台接口说明文档")
.version("1.0")
.build();
}
}
然后新建一个示例Controller。代码如下
package com.example.demo.Controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
@Api(value = “Sample”, description = “范例相关接口”,produces = MediaType.ALL_VALUE)
@Controller
public class UserController {
//restful接口示例
@ApiOperation(value = “欢迎页”, notes = “欢迎页信息”,httpMethod = “GET”,produces = MediaType.ALL_VALUE)
@GetMapping("/hello")
@ResponseBody
@ApiImplicitParams({
@ApiImplicitParam(name = “name”,value = “用户名字”,required =true,dataType =“String”),
@ApiImplicitParam(name = “text”,value = “内容”,required =true,dataType =“String”)
})
public Object helloRest(@RequestParam String name,
@RequestParam String text){
System.out.println(“hello restful!”);
HashMap<String,String> result = new HashMap<String,String>();
// result.put(“name”, HelloService.sayHello(name));
result.put(“text”, text);
result.put(“regard”, “regarding”);
return result;
}
}
3、运行测试
使用gradle的bootRun
打开网页测试,输入地址http://localhost:8080/swagger-ui.html#/,端口可能有不一样的,根据启动输入日志的端口来访问即可。
上面页面的Try it out还可以测试接口。
以上操作即可成功集成完毕。
后记,
总结下来,swagger自动生成的对代码的入侵确实比较多,详见Controller的代码片段。具体要不要用swagger,居于项目计划情况等酌情考虑
参考文档:
https://blog.csdn.net/qq_30070433/article/details/79174879