1.swagger接口文档自动化生成,方便调试,运行路径http://localhost:9700/doc.html 主机+端口号+doc.html ,老版本主机+端口号+swagger-ui.html ,运行效果如:
2.如何配置运用
a.pom.xml文件添加
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
com.github.xiaoymin
swagger-bootstrap-ui
1.8.7
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 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 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;
}