Springboot系列笔记之 整合Swagger
创建Springboot简单项目
导入依赖jar包
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
注意:此处引入的jar包和Springboot版本的兼容性,此处有坑。。。
编写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
class Swagger2Config {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.yusys.springboot01"))
.paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo(){
return new ApiInfoBuilder().title("Springboot整合Swagger2测试")
.description("Springboot创建RestfulAPI测试")
.termsOfServiceUrl("")
.contact("程序")
.version("1.0")
.build();
}
}
@Configuration:告诉Springboot该类为配置类。
@EnableSwagger2:告诉Springboot容器启用Swagger2功能。
创建Controller处理类
import com.yusys.springboot01.entity.Customer;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@RestController
@RequestMapping(value="/customers")
public class CustomerHandler {
//创建安全的线程
static Map<String, Customer> customerMap= Collections.synchronizedMap(new HashMap<String,Customer>());
/**
* 查询Customer列表
* @return
*/
@ApiOperation(value="查询Customer列表",notes="")
@RequestMapping(value="/",method = RequestMethod.GET)
public List<Customer> getCustomerList(){
System.out.println("开始执行getCustomerList()方法------");
//处理 /customers的请求,获取customer列表
List<Customer> list=new ArrayList<Customer>(customerMap.values());
return list;
}
/**
* 新增customer
* @param customer
* @return
*/
@ApiOperation(value="添加Customer",notes="根据customer信息添加Customer")
@ApiImplicitParam(name="customer",value = "customer详情",required = true,dataType = "Customer")
@RequestMapping(value="/",method = RequestMethod.POST)
public String addCustomer(@ModelAttribute Customer customer){
System.out.println("开始执行addCustomer(Customer customer)方法------");
customerMap.put(customer.getId(),customer);
return "success";
}
/**success
* 根据ID查询Customer
* @param id
* @return
*/
@ApiOperation(value="根据ID查询Customer",notes="根据ID查询Customer")
@ApiImplicitParam(name="id",value = "客户Id",required = true,dataType = "String",paramType = "path")
@RequestMapping(value="/{id}",method = RequestMethod.GET)
public Customer getCustomerById(@PathVariable String id){
System.out.println("开始执行getCustomerById(String id)方法------"+id);
return customerMap.get(id);
}
/**
*修改Customer信息
* @param id
* @param customer
* @return
*/
@ApiOperation(value="根据ID修改Customer信息",notes="根据ID修改Customer信息")
@ApiImplicitParams({
@ApiImplicitParam(name="id",value = "客户Id",required = true,dataType = "String",paramType = "path"),
@ApiImplicitParam(name="customer",value = "客户详细信息",required = true,dataType = "Customer")
})
@RequestMapping(value="/{id}",method = RequestMethod.PUT)
public String modifyCustomerById(@PathVariable String id,@ModelAttribute Customer customer){
System.out.println("开始执行modifyCustomerById()方法------");
Customer customer1=customerMap.get(id);
customer1.setName(customer.getName());
customer1.setAge(customer.getAge());
customerMap.put(id,customer1);
return "success";
}
/**
* 根据ID删除用户信息
* @param id
* @return
*/
@ApiOperation(value="根据ID修改Customer信息",notes="根据ID删除Customer信息")
@ApiImplicitParam(name="id",value = "客户Id",required = true,dataType = "String",paramType = "path")
@RequestMapping(value="/{id}",method = RequestMethod.DELETE)
public String deleteCustomerById(@PathVariable String id){
System.out.println("开始执行deleteCustomerById()方法------");
customerMap.remove(id);
return "success";
}
注意:@ApiImplicitParam(name=“id”,value = “客户Id”,required = true,dataType = “String”,paramType = “path”)
@RequestMapping(value="/{id}",method = RequestMethod.DELETE)
@PathVariable String id 的组合使用
此处 paramType = “path” 不添加的话参数传递可能会有问题。
创建测试用实体类
import org.springframework.stereotype.Component;
@Component
public class Customer {
private String id;
private String name;
private Integer age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
启动项目,确保工程无报错信息![启动工程](https://i-blog.csdnimg.cn/blog_migrate/4e94eb79be251af4d5e70893e22504ca.png)
** 注意:工程一定不能报错,不然无法进行后续测试 **
在浏览器地址栏输入地址查看生成的RestfulAPI可视化界面:
http://localhost:8080/swagger-ui.html