1、springboot 集成Swagger说明
Swagger挺好的,所以我用,省去了二次整理接口文档
2、操作步骤
2.1、配置pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
说明:其他web包不再演示
2.2、启动类配置
package cn.demo.mobile;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "cn.demo")
public class MobileWebApplication {
public static void main(String[] args) {
SpringApplication.run(MobileWebApplication.class, args);
}
}
2.3、配置SwaggerConfig(类名自定义)
package cn.demo.mobile;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author :yepk
* @version :1.0
* @apiNote :Swagger配置文件
* @date :2019-02-25-16:32
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket swaggerSpringMvcPlugin(){
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();
}
}
2.4、配置controller
package cn.demo.controller;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @author :yepk
* @version :1.0
* @apiNote :测试类
* @date :2019-02-25-16:35
*/
@Api(description = "测试接口")
@RestController
@RequestMapping(value = "demo")
public class DemoController {
@ApiOperation(value = "测试主页", notes = "测试主页接口")
@GetMapping(value = "/index", produces = "application/json;charset=utf-8")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户id", defaultValue = "1", required = true, paramType = "query", dataType = "Long", example = "1"),
@ApiImplicitParam(name = "userName", value = "用户名", defaultValue = "小明", required = true, paramType = "query", dataType = "String", example = "小明")
})
public String index(@RequestParam(value = "userId", required = true) Long userId,
@RequestParam(value = "userName", required = true) String userName) {
Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
map.put("user", userId);
map.put("userName", userName);
return JSON.toJSONString(map);
}
}
3、演示效果
按照启动之后访问 :http://localhost:20001/swagger-ui.html
如果打开找不到文档,在启动类扫描controller包,参照2.2