项目目录
pom依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
HomeController
import java.util.ArrayList;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@RestController
@Api(tags = "TestController") // 注意不要带中文,扩展时按钮无效
public class HomeController {
@ApiOperation(value="hello", notes="无参数GET请求测试")
@GetMapping("/hello")
public String hello() {
return "Hello World!";
}
@ApiOperation(value = "hello2", notes = "单简单参数")
@ApiImplicitParam(name = "name", value = "姓名", paramType = "query", dataType = "String")
@GetMapping("/hello2")
public String hello2(@RequestParam String name) {
return "Hello " + name;
}
@ApiOperation(value = "hello3", notes = "多简单参数")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "姓名", paramType = "query", dataType = "string"),
@ApiImplicitParam(name = "age", value = "年龄", paramType = "query", dataType = "int")
})
@GetMapping("/hello3")
public String hello3(@RequestParam String name, @RequestParam Integer age) {
return "Hello " + name + ", age " + age.toString();
}
@ApiOperation(value = "hello4", notes = "传递对象参数")
@ApiImplicitParam(name = "person", value = "人", paramType = "body", dataType = "Person")
@PostMapping("/hello4")
public String hello4(@RequestBody Person person) {
return "Hello, " + person.getName() + " " + person.getAge();
}
@ApiOperation(value = "hello5", notes = "传递集合参数")
@ApiImplicitParam(name = "persons", value = "集合列表", paramType = "body", dataType = "ArrayList<String>")
@PostMapping("/hello5")
public String hello5(@RequestBody ArrayList<String> persons) {
StringBuilder sb = new StringBuilder();
persons.forEach(t -> sb.append(t).append("\n"));
return sb.toString();
}
}
Person
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Person {
private String name;
private Integer age;
}
DemoSwaggerApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoSwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(DemoSwaggerApplication.class, args);
}
}
Swagger2
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;
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(this.apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.ljlin233.demoswagger.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("springboot利用swagger构建api文档")
.description("项目描述,代码地址 需要去我目录去找,随便放了个文件上传下载 https://download.youkuaiyun.com/download/qq_42981242/13244485")
.termsOfServiceUrl("https://download.youkuaiyun.com/download/qq_42981242/13244485")
.version("1.0")
.build();
}
}
application.properties没有东西上面配置好直接运行
http://localhost:8080/swagger-ui.html