<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2、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;
@Configuration
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.asiainfo.group"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot整合swagger2的测试项目")
.description("https://blog.csdn.net/xl_1803 by xulong")
.termsOfServiceUrl("https://blog.csdn.net/xl_1803")
.version("1.0")
.build();
}
}
3、启动类上打@EnableSwagger2注解
4、实体类
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
/**
* @author xulong3
* @Title: file_name
* @Package package_name
* @Description: todo
* @date 2020/5/24 15:57
*/
@Data
@ApiModel("商品对象")
public class Product implements Serializable {
private static final long serialVersionUID = 8170862039655345886L;
@ApiModelProperty("商品ID")
//@ApiParam(name="id",value="商品ID",required=true)
private Long id;
@ApiModelProperty("商品名称")
//@ApiParam(name="name",value="商品名称",required=true)
private String name;
@ApiModelProperty("商品数量")
//@ApiParam(name="amount",value="商品数量",required=true)
private Integer amount;
@ApiModelProperty("商品价格")
//@ApiParam(name="price",value="商品价格",required=true)
private BigDecimal price;
@ApiModelProperty("商品创建时间")
//@ApiParam(name="createTime",value="商品创建时间",required=false)
private Date createTime;
@ApiModelProperty("商品更新时间")
//@ApiParam(name="updateTime",value="商品更新时间",required=false)
private Date updateTime;
}
5、string转date类型转换器
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author xulong3
* @Title: file_name
* @Package package_name
* @Description: todo
* @date 2020/5/24 16:58
*/
@Component
public class String2DateConverter implements Converter<String, Date> {
@Override
public Date convert(String s) {
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
try {
return dateFormat.parse(s);
} catch (ParseException e) {
}
return null;
}
}
6、controller层统一返回对象
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author xulong3
* @Title: file_name
* @Package package_name
* @Description: todo
* @date 2020/5/24 16:07
*/
@Data
@ApiModel("通用响应对象")
public class WebResult<T> {
@ApiModelProperty("状态码")
private int code;
@ApiModelProperty("响应信息")
private String message;
@ApiModelProperty("响应数据")
private T data;
}
7、controller层
import com.asiainfo.group.springbootswagger2demo.entity.Product;
import com.asiainfo.group.springbootswagger2demo.util.WebResult;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author xulong3
* @Title: file_name
* @Package package_name
* @Description: todo
* @date 2020/5/24 16:05
*/
@RestController
@RequestMapping("/product")
@Api(tags = "商品controller")
public class ProductController {
@RequestMapping(value = "/save",method = RequestMethod.POST)
@ApiOperation(value = "新增商品")
@ApiImplicitParams(value={
@ApiImplicitParam(name="id",value="商品ID",required=true,paramType="query",dataType="int"),
@ApiImplicitParam(name="name",value="商品名称",required=true,paramType="query",dataType="string"),
@ApiImplicitParam(name="amount",value="商品数量",required=true,paramType="query",dataType="int"),
@ApiImplicitParam(name="price",value="商品价格",required=true,paramType="query",dataType="double"),
@ApiImplicitParam(name="createTime",value="商品创建时间",required=false,paramType="query",dataType="string"),
@ApiImplicitParam(name="updateTime",value="商品更新时间",required=false,paramType="query",dataType="string")
})
/*@ApiResponses({
@ApiResponse(code=200,message="200成功啦!"),
@ApiResponse(code=201,message="201啦!"),
@ApiResponse(code=400,message="400啦!"),
@ApiResponse(code=401,message="401啦!"),
@ApiResponse(code=403,message="403啦!"),
@ApiResponse(code=404,message="404啦!"),
@ApiResponse(code=500,message="500啦!")
})*/
public WebResult<String> save(@ApiIgnore Product product){
WebResult<String> wr = new WebResult<>();
wr.setCode(200);
wr.setMessage("success");
wr.setData("请求成功");
return wr;
}
@RequestMapping(value = "/queryAll",method = RequestMethod.GET)
@ApiOperation(value = "查询所有商品")
public WebResult<List<Product>> queryAll(){
WebResult<List<Product>> wr = new WebResult<>();
List<Product> productList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Product p = new Product();
p.setId((long)i);
p.setAmount(100);
p.setName("香蕉"+i);
p.setPrice(BigDecimal.valueOf(8.5));
p.setCreateTime(new Date());
p.setUpdateTime(new Date());
productList.add(p);
}
wr.setCode(200);
wr.setMessage("success");
wr.setData(productList);
return wr;
}
@RequestMapping(value = "/queryOne",method = RequestMethod.GET)
@ApiOperation(value = "查询单个商品")
@ApiImplicitParams(value={
@ApiImplicitParam(name="id",value="商品ID",required=true,paramType="query",dataType="int")
})
public WebResult<Product> queryOne(long id){
WebResult<Product> wr = new WebResult<>();
Product p = new Product();
p.setId(id);
p.setAmount(100);
p.setName("香蕉");
p.setPrice(BigDecimal.valueOf(8.5));
p.setCreateTime(new Date());
p.setUpdateTime(new Date());
wr.setCode(200);
wr.setMessage("success");
wr.setData(p);
return wr;
}
}
8、访问http://localhost:8080/swagger-ui.html