整合swagger2
- 引入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
- 添加配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket customDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.boo.web.controller"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("接口服务描述")
.version("1.0.0")
.build();
}
}
- 在接口上添加注解式描述
import com.boo.common.vo.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@Api("对外开放接口")
@RestController
@RequestMapping("/api")
public class OutApiController {
@ApiOperation("测试接口")
@ApiImplicitParam(name = "name",value = "姓名",dataType = "String")
@PostMapping("/test")
public static AjaxResult test(String name){
}
}
- 查看接口文档
启动服务,在浏览器访问【http://localhost:8080/swagger-ui.html】