springBoot整合swagger,测试restful风格 api - Go语言中文社区

springBoot整合swagger,测试restful风格 api


springBoot整合swagger,测试restful风格 api

  1. 引jar包
		<!--Swagger2的依赖-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>
  1. 编写配置类Swagger2
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {

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

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("hc-video相关接口")
                .description("本项目所有api还在开发中,请勿用于线上项目")
                .termsOfServiceUrl("http://www.baidu.com/")
                //作者
                .contact("你猜我是谁")
                //版本
                .version("1.0")
                .build();
    }

}
  1. ApiController
@RestController
@Api("swagger测试相关api")
@RequestMapping("test")
public class testController {

    @Autowired
    private GoodsService goodsService;

    @ApiOperation(value = "商品列表",notes = "获取所有商品信息,不分类")
    @GetMapping
    public JsonData getGoodslist(){
        return JsonData.buildSuccess(goodsService.findAll());
    }

    @ApiOperation(value = "新增商品信息", notes="根据商品对象创建商品")
    @ApiImplicitParam(name = "goods", value = "商品详细实体", required = true, dataType = "Goods")
    @PostMapping
    public JsonData addGoods(@RequestBody Goods goods){

        Integer flag = goodsService.add(goods);

        if(flag > 0){
            return JsonData.buildSuccess();
        }

        return JsonData.buildError("新增失败");

    }




    @ApiOperation(value="更新商品详细信息", notes="根据url的id来指定更新对象,并根据传过来的goods信息来更新商品详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer",paramType = "path"),
            @ApiImplicitParam(name = "goods", value = "商品详细实体user", required = true, dataType = "Goods")
    })
    @PutMapping(value="/{id}")
    public JsonData putGoods(@PathVariable Integer id, @RequestBody Goods goods) {
        Goods goodsDB = goodsService.findById(id);
        if(goodsDB != null){
            goodsDB.setName(goods.getName());
            goodsDB.setPrice(goods.getPrice());
        }

        Integer flag = goodsService.update(goodsDB);
        if(flag > 0){
            return JsonData.buildSuccess();
        }
        return JsonData.buildError("修改失败");
    }




    @ApiOperation(value="删除商品", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "Integer",paramType = "path")
    @DeleteMapping(value="/{id}")
    public JsonData deleteGoods(@PathVariable("id") Integer id) {
        System.out.println("进入方法");
        Integer flag = goodsService.delById(id);
        System.out.println("flag*****" + flag);

        if(flag > 0){
            return JsonData.buildSuccess();
        }
        return JsonData.buildError("操作失败");
    }



    @ApiOperation(value = "商品详情", notes = "根据url的id来查找指定对象")
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "Integer",paramType = "path")
    @GetMapping(value= "/{id}")
    public JsonData findInfo(@PathVariable Integer id){
        Goods goods = goodsService.findById(id);
        if(goods != null){
            return JsonData.buildSuccess(goods);
        }

        return JsonData.buildError("未发现此商品存在");
    }

}
  1. 启动自己的项目, 访问 http://localhost:8081/swagger-ui.html, 出现如下页面,说明整合成功
    在这里插入图片描述
    在这里插入图片描述
    restful 风格api最大的好处就是我不用再去关注具体增删改查的名称了,不在具体到某一条信息了,我只需要去关注test本身, 参数有哪些, methd是get,post,put,还是delete,

最常用的注解

@Api("swagger测试相关api"):
作用在类上, 修饰整个类,描述整个Controller的作用

@ApiOperation(value = "商品详情", notes = "根据url的id来查找指定对象"):
作用在方法上, 或者说一个接口 , 属性value描述该方法作用,notes描述方法的具体过程

@ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "Integer",paramType = "path"):
作用在方法上, 或者说一个接口 ,单个参数的描述,属性name参数名称,value描述该方法作用,required参数是否必须, 
	注意: dataType数据类型, paramType 描述参数在什么地方
	dataType="int" 代表请求参数类型为int类型,当然也可以是Map、User、String等;
	paramType="body" 代表参数应该放在请求的什么地方:
		header-->放在请求头。请求参数的获取:@RequestHeader(代码中接收注解)
		query-->用于get请求的参数拼接。请求参数的获取:@RequestParam(代码中接收注解)
		path(用于restful接口)-->请求参数的获取:@PathVariable(代码中接收注解)
		body-->放在请求体。请求参数的获取:@RequestBody(代码中接收注解)
		form(不常用)
paramType不写的话貌似默认是body, 我在这里填坑1小时

@ApiImplicitParams:如果有多个参数的话 如下用法:
	@ApiOperation(value="更新商品详细信息", notes="根据url的id来指定更新对象,并根据传过来的goods信息来更新商品详细信息")
	@ApiImplicitParams ({
        @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer",paramType = "path"),
        @ApiImplicitParam(name = "goods", value = "商品详细实体user", required = true, dataType = "Goods")
	})
	@PutMapping(value="/{id}")
	public JsonData putGoods(@PathVariable Integer id, @RequestBody Goods goods) {
  
	}

在这里插入图片描述
还有其他注解大家自己去了解吧, 这就基本可以使用swagger了

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_43606738/article/details/100079799
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢