最新swagger ui样式使用指南

1.swagger接口文档自动化生成,方便调试,运行路径http://localhost:9700/doc.html  主机+端口号+doc.html  ,老版本主机+端口号+swagger-ui.html  ,运行效果如:效果图

2.如何配置运用
a.pom.xml文件添加

 <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>

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.8.7</version>
        </dependency>

 b.  SwaggerConfig配置类
 

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("小程序服务端API接口文档")
                .description("小程序服务端")
                .version("1.0.1")
                .build();
    }

}

c.Application注解配置启动

@SpringBootApplication
@EnableScheduling
@EnableBaseCore
//@EnableSimpleVerification
@EnableSwagger2
public class DrawApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(DrawApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DrawApplication.class);
    }
}

d.常用注解说明

@Api:放在类的控制模块
@ApiOperation:放在接口方法上
@ApiImplicitParams:方法上一组参数说明
@ApiImplicitParam:一个参数的说明
@ApiResponses:一组响应说明
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
@ApiModel:请求体和返回体说明
@ApiModelProperty:请求体属性说明

e.代码块演示:

控制模块

@RequestMapping("api/random")
@RestController
@Api(value = "api/random", tags = {"抽奖模块"}, description = "随机的控制层")
public class RandomController extends BaseController {

    @Resource
    private RandomService randomService;


    /**
     * 随机抽奖
     *
     * @param data
     * @param check
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/lucky", method = RequestMethod.POST)
    @SignVerification
    @ApiOperation(value = "抽奖", notes = "根据用户id,随机产生了一个类型")
    public ApiResponse lucky(@RequestBody @Validated ApiRequest<LuckyReq> data, BindingResult check) {
        super.checkParameters(check);
        ApiResponse result = ApiResponse.buildSuccess();
        LuckyResp luck = this.randomService.luck(data.getData());
        result.put("data", luck);
        return result;
    }

    /**
     * 随机抽奖2
     */
    @ResponseBody
    @RequestMapping(value = "/lucky2", method = RequestMethod.POST)
    @SignVerification
    @ApiOperation(value = "抽奖2", notes = "根据用户id,随机产生了一个类型")
    @ApiImplicitParam(paramType = "query", name = "userId", value = "用户ID", required = true, dataType = "String")
    public LuckyResp lucky2(@RequestParam(value = "userId") String userId) {
        LuckyResp luck = this.randomService.luck2(userId);
        return luck;
    }

    /**
     * 随机抽奖4
     */
    @ResponseBody
    @RequestMapping(value = "/lucky4", method = RequestMethod.POST)
    @SignVerification
    @ApiOperation(value = "抽奖4", notes = "根据用户id,随机产生了一个类型")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "类型", required = true, dataType = "int")
    })
    public LuckyResp lucky4(@RequestParam(value = "userId") String userId,
                            @RequestParam(value = "type")  int type) {
        LuckyResp luck = this.randomService.luck4(userId,type);
        return luck;
    }


    /**
     * 随机抽奖3
     */
    @ResponseBody
    @RequestMapping(value = "/lucky3", method = RequestMethod.POST)
    @SignVerification
    @ApiOperation(value = "抽奖3", notes = "根据用户id,随机产生了一个类型")
    public LuckyResp lucky3(@RequestBody LuckyReq data) {
        LuckyResp luck = this.randomService.luck(data);
        return luck;
    }

    /**
     * 打开抽奖 抽话费
     *
     * @param data
     * @param check
     * @return
     */
    @RequestMapping(value = "/open", method = RequestMethod.POST)
    @SignVerification
    @ApiOperation(value = "开奖", notes = "----")
    public ApiResponse open(@RequestBody @Validated ApiRequest<OpenReq> data, BindingResult check) {
        checkParameters(check);
        ApiResponse result = ApiResponse.buildSuccess();
        OpenResp luck = this.randomService.open(data.getData());
        result.put("data", luck);
        return result;
    }

    /**
     * banner图随机跳转一个游戏
     *
     * @return
     */
    @RequestMapping(value = "/game", method = RequestMethod.GET)
    @ApiOperation(value = "随机游戏", notes = "-----")
    public ApiResponse game() {
        ApiResponse result = ApiResponse.buildSuccess();
        Game game = this.randomService.game();
        result.put("data", game);
        return result;
    }

    /**
     * 随机产生一个商品
     *
     * @return
     */
    @RequestMapping(value = "/good", method = RequestMethod.GET)
    @ApiOperation(value = "随机商品", notes = "----")
    public ApiResponse good() {
        ApiResponse result = ApiResponse.buildSuccess();
        Goods good = this.randomService.good();
        result.put("data", good);
        return result;
    }

请求模块
 

@Data
@ApiModel(value = "抽奖请求体")
public class LuckyReq {
    @NotEmpty(message = "用户id不能为空")
    @ApiModelProperty(value = "用户ID",required = true)
    private String userId;
}

响应模块

@Data
@ApiModel(value = "开奖返回值")
public class LuckyResp {
    /**
     * 抽奖类型
     */
    @ApiModelProperty(value = "抽奖类型")
    private Integer type;
    /**
     * 抽奖存入的id
     */
    @ApiModelProperty(value = "抽奖记录id")
    private Integer id;

    /**
     * 抽到钻石数量
     */
    @ApiModelProperty(value = "抽奖的钻石数量,没有返回null")
    private Integer diamondNum;
}

f:执行效果
执行效果

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值