4.0.0
com.learn
swagger
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
org.springframework.boot
spring-boot-starter-web
io.springfox
springfox-swagger2
2.8.0
io.springfox
springfox-swagger-ui
2.8.0
package com.learn.swagger;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* swagger 配置信息
*/
@Component
@ConfigurationProperties(prefix = "swagger")
public class SwaggerInfo {
private String groupName ="controller";
private String basePackage;
private String antPath;
private String title = "HTTP API";
private String description = "Swagger 自动生成接口文档";
private String license = "Apache License Version 2.0";
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getBasePackage() {
return basePackage;
}
public void setBasePackage(String basePackage) {
this.basePackage = basePackage;
}
public String getAntPath() {
return antPath;
}
public void setAntPath(String antPath) {
this.antPath = antPath;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLicense() {
return license;
}
public void setLicense(String license) {
this.license = license;
}
}
package com.learn.swagger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
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.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* swagger 配置类
*/
@Configuration
@ComponentScan(basePackages = "com.learn.swagger")
@EnableSwagger2
public class SwaggerConfiguration {
@Autowired
private SwaggerInfo swaggerInfo;
@Bean
public Docket controllerApi() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName(swaggerInfo.getGroupName())
.apiInfo(apiInfo());
ApiSelectorBuilder builder = docket.select();
if (!StringUtils.isEmpty(swaggerInfo.getBasePackage())) {
builder = builder.apis(RequestHandlerSelectors.basePackage(swaggerInfo.getBasePackage()));
}
if (!StringUtils.isEmpty(swaggerInfo.getAntPath())) {
builder = builder.paths(PathSelectors.ant(swaggerInfo.getAntPath()));
}
return builder.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(swaggerInfo.getTitle())
.description(swaggerInfo.getDescription())
.termsOfServiceUrl("http://springfox.io")
.contact("learn")
.license(swaggerInfo.getLicense())
.licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")
.version("2.0")
.build();
}
}
package com.learn.swagger;
import org.springframework.context.annotation.Import;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/**
* 开启swagger文档自动生成功能
*/
@Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(value = { java.lang.annotation.ElementType.TYPE })
@Documented
@Import(SwaggerConfiguration.class)
@EnableSwagger2
public @interface EnableMySwagger {
}
package com.learn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AppSwagger2 {
public static void main(String[] args) {
SpringApplication.run(AppSwagger2.class, args);
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.learn.swagger.SwaggerConfiguration
Swagger UI
package com.learn.entity;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
/**
* 产品
*/
@Entity
public class Product implements Serializable{
@Id
private String id;
private String name;
/**
* @see com.learn.entity.enums.ProductStatus
*/
@ApiModelProperty(value = "状态",dataType = "com.learn.entity.enums.ProductStatus")
private String status ;
//起投金额
private BigDecimal thresholdAmount ;
//投资步长
private BigDecimal stepAmount;
//锁定期
private Integer lockTerm;
//收益率,因为要与其他数相乘,所以使用BigDecimal
private BigDecimal rewardRate;
private String memo;
private Date createAt;
private Date updateAt;
private String createUser;
private String updateUser;
public Product(){}
public Product(String id, String name, String status, BigDecimal thresholdAmount, BigDecimal stepAmount, BigDecimal rewardRate) {
this.id = id;
this.name = name;
this.status = status;
this.thresholdAmount = thresholdAmount;
this.stepAmount = stepAmount;
this.rewardRate = rewardRate;
}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
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 String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public BigDecimal getThresholdAmount() {
return thresholdAmount;
}
public void setThresholdAmount(BigDecimal thresholdAmount) {
this.thresholdAmount = thresholdAmount;
}
public BigDecimal getStepAmount() {
return stepAmount;
}
public void setStepAmount(BigDecimal stepAmount) {
this.stepAmount = stepAmount;
}
public Integer getLockTerm() {
return lockTerm;
}
public void setLockTerm(Integer lockTerm) {
this.lockTerm = lockTerm;
}
public BigDecimal getRewardRate() {
return rewardRate;
}
public void setRewardRate(BigDecimal rewardRate) {
this.rewardRate = rewardRate;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
public Date getCreateAt() {
return createAt;
}
public void setCreateAt(Date createAt) {
this.createAt = createAt;
}
public Date getUpdateAt() {
return updateAt;
}
public void setUpdateAt(Date updateAt) {
this.updateAt = updateAt;
}
public String getCreateUser() {
return createUser;
}
public void setCreateUser(String createUser) {
this.createUser = createUser;
}
public String getUpdateUser() {
return updateUser;
}
public void setUpdateUser(String updateUser) {
this.updateUser = updateUser;
}
}
package com.learn.manager.controller;
import com.learn.entity.Product;
import com.learn.manager.service.ProductService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
/**
* 产品
*/
@RestController
@RequestMapping("/products")
public class ProductController {
private static Logger LOG = LoggerFactory.getLogger(ProductController.class);
@Autowired
private ProductService service;
@ApiOperation(value = "创建产品",notes = "根据对应业务规则添加相应的产品")
@RequestMapping(value = "", method = RequestMethod.POST)
public Product addProduct(@RequestBody Product product) {
LOG.info("创建产品,参数:{}", product);
Product result = service.addProduct(product);
LOG.info("创建产品,结果:{}", result);
return result;
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Product findOne(@PathVariable String id) {
LOG.info("查询单个产品,id={}", id);
Product product = service.findOne(id);
LOG.info("查询单个产品,结果={}", product);
return product;
}
@RequestMapping(value = "", method = RequestMethod.GET)
public Page query(String ids, BigDecimal minRewardRate, BigDecimal maxRewardRate, String status,
@RequestParam(defaultValue = "0") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
LOG.info("查询产品,ids={},minRewardRate={},maxRewardRate={},status,pageNum={},pageSize={}");
List idList = null, statusList = null;
if (!StringUtils.isEmpty(ids)) {
idList = Arrays.asList(ids.split(","));
}
if (!StringUtils.isEmpty(status)) {
statusList = Arrays.asList(status.split(","));
}
Pageable pageable = new PageRequest(pageNum, pageSize);
Page page = service.query(idList, minRewardRate, maxRewardRate, statusList, pageable);
LOG.info("查询产品,结果={}", page);
return page;
}
}
package com.learn.entity.enums;
/**
* 产品状态
*/
public enum ProductStatus {
AUDITING("审核中"),
IN_SELL("销售中"),
LOCKED("暂停销售"),
FINISHED("已结束");
private String desc;
ProductStatus(String desc){
this.desc = desc;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
http://localhost:8080/swagger-ui.html