美文网首页
swagger配置

swagger配置

作者: 与乐为乐 | 来源:发表于2020-08-17 20:07 被阅读0次
    1. pom.xml
      <!--swagger配置-->
          <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
            </dependency>
            <dependency>
                <groupId>com.github.xiaoymin</groupId>
                <artifactId>swagger-bootstrap-ui</artifactId>
                <version>1.9.6</version>
            </dependency>
        <!--lombok配置-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.10</version>
            </dependency>
    
    
    2. Swagger2.java

    放在SpringBoot启动类同级目录

    import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.bind.annotation.RequestMethod;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.ResponseMessage;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    import java.util.ArrayList;
    import java.util.List;
    
    @Configuration
    @EnableSwagger2
    @EnableSwaggerBootstrapUI
    public class Swagger2 {
        
        @Bean
        public Docket defaultApi() {
            List<ResponseMessage> responseMessageList = new ArrayList<>();
            EnumSet<GlobalsEnums> set = EnumSet.allOf(GlobalsEnums.class);
            for (GlobalsEnums s : set) {
                responseMessageList.add(new ResponseMessageBuilder().code(Integer.valueOf(s.getCode())).message(s.getMsg()).build());
            }
            Docket docket = new Docket(DocumentationType.SWAGGER_2);
            docket.globalResponseMessage(RequestMethod.POST, responseMessageList)
                    .apiInfo(apiInfo())
                    .groupName("***公司")
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.controller--修改"))
                    .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                    .paths(PathSelectors.any())
                    .build();
            return docket;
    
        }
    
        private ApiInfo apiInfo () {
            return new ApiInfoBuilder ()
                    .title ("接口API")
                    .contact("***有限公司")
                    .description ("数据交互标准API")
                    .termsOfServiceUrl ("https://www.baidu.com/")
                    .version ("1.0")
                    .build ();
        }
    
    
    }
    
    
    3. Controller层
    import com.Response.BaseResponse;
    import com.request.TradeRequest;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    import javax.validation.Valid;
    /**
     * @Description: *API
     * @Author: ***
     * @Date: ***
     * @Version: *
     */
    @RestController
    @RequestMapping("/openApi/trade")
    @Api(value = "TradeController", tags = "订单API", description = "提供订单操作相关能力")
    public class TradeController {
    
        @RequestMapping(method = RequestMethod.POST, path = "/*")
        @ApiOperation(value = "*", notes = "*")
        public BaseResponse * (@RequestBody @Valid TradeRequest request)  throws Exception{
            return null;
        }
    
        @RequestMapping(method = RequestMethod.POST, path = "/*")
        @ApiOperation(value = "*", notes = "*")
        public BaseResponse * (@RequestBody @Valid TradeRequest request)  throws Exception{
            return null;
        }
    
        @RequestMapping(method = RequestMethod.POST, path = "/*")
        @ApiOperation(value = "*", notes = "*")
        public BaseResponse * (@RequestBody @Valid TradeRequest request)  throws Exception{
            return null;
        }
    
        @RequestMapping(method = RequestMethod.POST, path = "/*")
        @ApiOperation(value ="*", notes = "*")
        public BaseResponse * (@RequestBody @Valid TradeRequest request)  throws Exception{
            return null;
        }
    
    
    }
    
    4. Entity
    4.1. ***Request
    import com.fasterxml.jackson.annotation.JsonFormat;
    import com.entity.Orders;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Getter;
    import lombok.Setter;
    import org.hibernate.validator.constraints.Length;
    import org.hibernate.validator.constraints.Range;
    import org.hibernate.validator.constraints.URL;
    import org.springframework.format.annotation.DateTimeFormat;
    
    import javax.validation.Valid;
    import javax.validation.constraints.*;
    import java.math.BigDecimal;
    import java.util.Date;
    import java.util.List;
    
    /**
     * @Description: 订单创建请求参数(下单)
     * @Author: xiong
     * @Date: 2020/7/30
     * @Version: 1.0
     */
    @Setter
    @Getter
    public class TradeRequest extends BaseRequest{
        @ApiModelProperty(value = "开始时间", name = "startTime", required = true)
        @NotNull(message = "开始时间不能为空")
        private Date startTime;
    
        @ApiModelProperty(value = "结束时间", name = "endTime", required = true)
        @NotNull(message = "结束时间不能为空")
        private Date endTime;
    
        @ApiModelProperty(value = "账单汇总类型: 1-日汇总,2-月汇总", name = "summaryType", required = true, position = 22)
        @Range(min = 1, max = 2, message = "账单汇总类型为1或2,  1-日汇总,2-月汇总")
        @NotNull(message = "summaryTime 不允许为空")
        private Integer summaryType;
    
        @ApiModelProperty(value="账单日汇总时间(默认格式化yyyy-MM, 汇总类型为日汇总时必填)", name="day", position = 23)
        @DateTimeFormat(pattern = "yyyy-MM")
        @JsonFormat(pattern = "yyyy-MM", locale = "zh", timezone = "GMT+8")
        private Date day;
    
        @ApiModelProperty(value = "第三方支付订单号(最大长度50)", name = "thirdPayTradeId", required = true, position = 40)
        @NotBlank(message = "第三方支付订单号不能为空")
        @Length(max = 50, message = "第三方支付订单号长度不能超过50个字符")
        private String thirdPayTradeId;
    
        @ApiModelProperty(value = "商品总金额(值>=0)", name = "totalFee", position = 60)
        @DecimalMin(value = "0", message = "商品总金额不能小于0")
        @NotNull(message = "商品总金额不能为空")
        private BigDecimal totalFee;
    
        @ApiModelProperty(value = "买家邮箱", name = "buyerEmail", position = 80)
        @Email(message = "买家邮箱格式不对")
        private String buyerEmail;
    
        @ApiModelProperty(value = "商品图片地址(必须以http://或者https://开头)", name = "picPath")
        @URL(message = "商品图片地址格式不合法")
        @Length(min = 0, max = 200, message = "商品图片地址的长度不能超过200个字符")
        private String picPath;
    
        @ApiModelProperty(value = "付款超时秒数(单位:秒,最小1秒,最大2天)", name = "timeoutSecond", required = true, position = 91)
        @NotNull(message = "付款超时秒数不能为空")
        @Range(min = 1, max = 172800, message = "付款超时秒数最小1秒,但不能超过2天")
        private Integer timeoutSecond;
    
        @ApiModelProperty(value = "子订单集合", name = "orders", required = true, position = 200)
        @Valid
        @NotNull(message = "子订单不允许为空")
        @NotEmpty(message = "子订单不允许为空")
        private List<Orders> orders;
    
    
    }
    
    
    4.2.***Response
    import com.Enums.GlobalsEnums;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Getter;
    import lombok.Setter;
    import java.io.Serializable;
    /**
     * @Description: 响应基类
     * @Author: ***
     * @Date: ***
     */
    @Setter
    @Getter
    public abstract class BaseResponse<T> implements Serializable {
    
        @ApiModelProperty(value = "响应编码", position = 1)
        private String code = GlobalsEnums.SUCCESS.getCode ();
    
        @ApiModelProperty(value = "响应描述", position = 2)
        private String msg = GlobalsEnums.SUCCESS.getMsg ();
    
        @ApiModelProperty(value = "响应状态(true:成功 false:失败)", position = 3)
        public Boolean success = true;
    
        @ApiModelProperty(value = "响应结果", position = 4)
        private T result;
    }
    
    

    访问:http://localhost:8080/doc.html
    http://localhost:8080/swagger-ui.html

    相关文章

      网友评论

          本文标题:swagger配置

          本文链接:https://www.haomeiwen.com/subject/fggbdktx.html