一、相关依赖
http://localhost:8080/swagger-ui.html http://localhost:8080/swagger-ui.html
1.通过Swagger给一些比较难理解的属性或者接口,增加注释信息
2.接口文档实时更新
3.可以在线测试
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-common -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
Swagger类
package wagger.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.support.RequestHandledEvent;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableSwagger2
public class swaggerconfig {
//配置swagger bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
指定扫描的包
.apis(RequestHandlerSelectors.basePackage("wagger.demo.controller"))
// .apis(RequestHandlerSelectors.any()) //扫描所有
// .apis(RequestHandlerSelectors.none()) //都不扫描
// .apis(RequestHandlerSelectors.withClassAnnotation("") //扫描类上的注解
// .apis(RequestHandlerSelectors.withMethodAnnotation() //扫描方法上的注解
.build()
.groupName("xiao")
;
}
@Bean
public Docket docke1(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("a")
;
}
private ApiInfo apiInfo(){
//信息
Contact contact = new Contact("KUANGSN", "https://www.bilibili.com/video/BV1Y441197Lw?p=2&spm_id_from=pageDriver", "");
return new ApiInfo("修小的文档", "MONEY HONEY",
"1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
}
实体类
package wagger.demo.pojo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用户")
public class User {
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}
控制类
package wagger.demo.controller;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wagger.demo.pojo.User;
@RestController
public class hellowcontroller {
@RequestMapping( "/hello")
public String hello(){
return "hello";
}
@PostMapping( "/user")
public User user(){
return new User() ;
}
@ApiOperation("Hello控制类") //方法
@GetMapping( "/hello2")
public String hello2(@ApiParam("用户名") String username){
return "hello2";
}
}