Spring Swagger注解生成接口文档

Spring Swagger通过注解就可以为我们生成接口文档,十分便捷!

首先,在build.grade配置文件中加入依赖

implementation 'org.apache.kafka:kafka-clients'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'

加入Swagger的配置

/**
 * @author linzihao
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "swagger2", value = {"enable"}, havingValue = "true")
public class Swagger2Config {

    //http://localhost:7002/data-vip/doc.html
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).pathMapping("/").select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .title("数据规则API接口定义").description("数据规则API接口定义......").version("1.0")
                        .contact(new Contact("数据规则API接口定义", "www.ligeit.com", "[email protected]")).license("数据规则API接口定义")
                        .licenseUrl("https://www.ligeit.com").build());
        // .globalOperationParameters(pars);
    }
}

使用Swagger的注解定义接口的信息:

/**
 *
 * @author Johny 林子豪
 */
@Getter
@Setter
@Slf4j
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("售后订单")
public class AfterSellRequest extends BaseRequest {
    private static final long serialVersionUID = 8911378473448417126L;
    /**
     * 订单编码(订单流水号)--
     */
    @ApiModelProperty(value = "订单编码",dataType = "String",required = true)
    @JsonProperty("order_code")
    private String orderCode;
    /**
     * 订单明细id --
     */
    @ApiModelProperty(value = "订单id",dataType = "Long",required = true)
    @JsonProperty("order_detail_id")
    private Long orderDetailId;
    /**
     * 服务单号 --
     */
    @ApiModelProperty(value = "服务单号",dataType = "String",required = true)
    @JsonProperty("order_service_no")
    private String orderServiceNo;
    /**
     * 商家渠道(如JD,华泊) --
     */
    @ApiModelProperty(value = "商家渠道(如JD,华泊)",dataType = "String",required = true)
    @JsonProperty("business_channel")
    private String businessChannel;
    /**
     * 退款金额 --
     */
    @ApiModelProperty(value = "退款金额",dataType = "Long",required = true)
    @JsonProperty("order_refund_amount")
    private Long orderRefundAmount;
    /**
     * 会员申请金额 --
     */
    @ApiModelProperty(value = "会员申请金额",dataType = "Long")
    @JsonProperty("member_order_refund_amount")
    private Long memberOrderRefundAmount;
    /**
     * 运费
     */
    @ApiModelProperty(value = "运费",dataType = "Long")
    @JsonProperty("order_post_amount")
    private Long orderPostAmount;
    /**
     * 退货数量 --
     */
    @ApiModelProperty(value = "退货数量",dataType = "Integer",required = true)
    @JsonProperty("order_refund_count")
    private Integer orderRefundCount;
    /**
     * 图片集合----凭证,非必传
     */
    @ApiModelProperty(value = "图片集合----凭证",dataType = "List")
    private List urls;

    /**
     * 售后留言
     */
    @ApiModelProperty(value = "售后留言",dataType = "String")
    private String remarks;
    /**
     * 售后原因id(退款原因id) --
     */
    @ApiModelProperty(value = "售后原因id(退款原因id)",dataType = "Long",required = true)
    @JsonProperty("refund_reason_code")
    private Long refundReasonCode;
    /**
     * 售后原因  --
     */
    @ApiModelProperty(value = "售后原因",dataType = "String",required = true)
    @JsonProperty("refund_reason_name")
    private String refundReasonName;

    public Boolean statusValid(String createAfterSellOrderStatus) {
        return !Objects.equals(createAfterSellOrderStatus, ServiceOrderStatusEnum.SERVICE_ORDER_SUCCESS_PUSH.getCode());
    }

    public String valid() {
        StringBuilder sb = new StringBuilder();
        sb.append("---创建服务单请求参数校验---  ");
        if (StringUtils.isEmpty(this.orderCode)) {
            log.info("元订单id不合法,元订单id:[{}]", this.orderCode);
            sb.append(" 元订单id不合法,元订单id:").append(this.orderCode);
        }
        if (Objects.isNull(this.orderDetailId)) {
            log.info("子订单id不合法,[{}]", this.orderDetailId);
            sb.append(" 子订单id不合法,团巴拉子订单号:").append(this.orderDetailId);
        }
        AfterSellLogisticsRequest.handle(sb, this.orderServiceNo, this.businessChannel);
        if (Objects.isNull(this.orderRefundAmount)) {
            log.info("退款金额不合法,[{}]", this.orderRefundAmount);
            sb.append(" 退款金额不合法,退款金额:").append(this.orderRefundAmount);
        }
        if (Objects.isNull(this.orderPostAmount)) {
            log.info("邮费不合法,[{}]", this.orderPostAmount);
            sb.append(" 邮费不合法,邮费:").append(this.orderPostAmount);
        }
        if (Objects.isNull(this.refundReasonCode)) {
            log.info("退货原因id不合法,[{}]", this.refundReasonCode);
            sb.append(" 退货原因id不合法,退货原因id:").append(this.refundReasonCode);
        }
        if (StringUtils.isEmpty(this.refundReasonName)) {
            log.info("退货原因不合法,[{}]", this.refundReasonName);
            sb.append(" 退货原因不合法,退货原因:").append(this.refundReasonName);
        }
        return sb.toString();
    }
}

启动服务器就会生成接口文档。

http://localhost:7002/data-vip/doc.html

Spring Swagger注解生成接口文档_第1张图片

你可能感兴趣的:(java,接口文档,Spring,Swagger,restful)