对于许多开发来说,编写这个yml或json格式的描述文件,本身也是有一定负担的工作,特别是在后面持续迭代开发的时候,往往会忽略更新这个描述文件,直接更改代码。久而久之,这个描述文件也和实际项目渐行渐远,基于该描述文件生成的接口文档也失去了参考意义。所以作为Java届服务端的大一统框架Spring,迅速将Swagger规范纳入自身的标准,建立了Spring-swagger项目,后面改成了现在的Springfox。通过在项目中引入Springfox,可以扫描相关的代码,生成该描述文件,进而生成与代码一致的接口文档和客户端代码。
1、返回码信息列表
springboot整合swagger
1、添加pom依赖
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</dependency>
2、创建swaggerconfig类
package com.seckill.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author m
* @className SwaggerConfig
* @description SwaggerConfig
* @date 2020/6/1
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.seckill.controller"))
.paths(PathSelectors.any())
.build();
}
/**
* 构建API文档的详细信息函数
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("miaosha项目RESTful APIs")
//创建人
.contact(new Contact("babbo","127.0.0.1:8080","null"))
//描述
.description("miaosha项目后台api接口文档")
//版本号
.version("1.0")
.build();
}
}
3、在controller中添加注解的方式来创建api
@Api(value = "登录",description = "登录结果")
@Controller
public class LoginController {
![Z$~NS$AA]{9Y8ZZ953{V$25.png](https://upload-images.jianshu.io/upload_images/23363658-eba94b773193423b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
@Autowired
MiaoshaUserService miaoshaUserService;
private static Logger log= LoggerFactory.getLogger(LoginController.class);
@RequestMapping("/tologin")
public String toLogin(){
return "login";
}
@RequestMapping("/dologin")
@ApiOperation("登录")
@ApiResponse(code=100,message = "成功")
public Msg doLogin(HttpServletResponse response, @Valid LoginVo loginVo){
log.info(loginVo.toString());
//登录
miaoshaUserService.login(response,loginVo);
return Msg.success();
}
秒杀controller
@Api(value = "秒杀",description = "秒杀接口")
@Controller
@RequestMapping("/miaosha")
public class MiaoshaController {
@Autowired
RedisService redisService;
@Autowired
GoodsService goodsService;
@Autowired
MiaoshaUserService miaoshaUserService;
@Autowired
OrderService orderService;
@Autowired
MiaoshaService miaoshaService;
@ApiOperation(httpMethod = "POST",value = "秒杀",notes = "立即秒杀")
@ApiImplicitParam(name = "id",value = "商品id",required = true ,dataType = "Long")
@RequestMapping("/domiaosha")
public String list(
Model model, MiaoshaUser user,
@RequestParam("goodsId")long goodsId){
model.addAttribute("user",user);
if (user==null){
return "login";
}
//判断库存
GoodsVo goods=goodsService.getGoodVoByGoodsId(goodsId);
int stock=goods.getStockCount();
if (stock<=0){
model.addAttribute("errmsg",Msg.miaoshaover().getMsg());
return "miaosha_fail";
}
//判断是否已经秒杀成功过
MiaoshaOrder order=orderService.getMiaoshaOrderByUserIdGoodsId(user.getId(),goodsId);
if (order!=null){
model.addAttribute("errmsg",Msg.repeatemiaosha().getMsg());
return "miaosha_fail";
}
//减库存,下订单,写入秒杀订单
OrderInfo orderInfo=miaoshaService.miaosha(user,goods);
model.addAttribute("orderInfo",orderInfo);
model.addAttribute("goods",goods);
return "order_detail";
}
4、启动秒杀项目,
访问swagger-ui.html
如果测试某个功能,则可以进行点击进入,输入相应的数据,即可测试