springCloud+Vue改造品优购第三章--------引入接口文档测试工具Swagger

第一步 加入依赖

        <!-- swagger文档 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>

第二步 创建配置类

package com.pyg.manage.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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class Swagger2Config {

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

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("品优购商品服务API文档")
                .description("简单优雅的restfun风格")
                .termsOfServiceUrl("http://localhost:8002/swagger-ui.html")
                .version("1.0")
                .build();
    }


}

第三步 启动类上加注解开启swagger2

package com.pyg.manage;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
@ComponentScan(basePackages = {"com.pyg"})
public class ManageServiceApp {

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

第四步 开发RestfulAPI

package com.pyg.manage.controller;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.pyg.base.controller.BaseController;
import com.pyg.entity.Result;
import com.pyg.entity.TbBrand;
import com.pyg.entity.TbBrandExample;
import com.pyg.service.BrandService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController{

    private Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Autowired
    private BrandService brandService;
    @ApiOperation(value = "查询全部品牌信息列表", notes = "返回品牌信息list,可以根据属性查询")
    @GetMapping("/list")
    public Page<TbBrand> list(TbBrandExample brandExample){
        return brandService.pageList(brandExample);
    }
    @ApiOperation(value = "分页查询,默认每页十条数据", notes = "可根据Map封装参数进行条件查询")
    @GetMapping("/page")
    public Page<Map<String, Object>> page(){
        logger.info("provider--->queryMap[ "+this.getParams().toString()+" ]");
        return brandService.getPage(this.getParams());
    }

    @ApiOperation(value = "根据ID查询品牌信息", notes = "type=1查询详情,type=2,回显编辑的信息")
    @ApiImplicitParams({//传递的参数说明,该类型的参数是拼接在url后面的
        @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "type", value = "type", required = true, dataType = "int", paramType = "query") })
    @GetMapping("/info")
    public Result getInfo(int type, long id){
        Result result = null;
        try {
            result = Result.success(brandService.getInfo(type, id));
            return result;
        } catch (Exception e) {
            result  = Result.failure(500, e.getMessage(), null);
            logger.error("根据ID查询品牌信息失败<--->result:"+result.toString());
            return result;
        }
    }

    @ApiOperation(value = "编辑新增通用API", notes = "当ID为空的时候新增,ID不为空的时候修改")
    @PostMapping("/edit")
    public Result edit(TbBrand tbBrand) {
        try {
            Map<String, Object> returnMap = brandService.edit(tbBrand);
            return Result.success(returnMap);
        } catch (Exception e) {
            Result result = Result.failure(500, e.getMessage(), null);
            logger.error("修改品牌信息失败<--->result:"+result.toString());
            return result;
        }

    }

    @ApiOperation(value = "单个删除/批量删除通用API", notes = "当List只有一条数据删除单个,当多条数据逐个删除")
    @PostMapping("/del")
    public Result del(List<Long>[] ids) {
        try {
            Integer delCount = brandService.delBeatch(ids);
            return Result.success(delCount);
        } catch (Exception e) {
            Result result = Result.failure(500, e.getMessage(), null);
            logger.error("删除品牌信息失败<--->result:"+result.toString());
            return result;
        }

    }
}

第五步 访问Restful API

品牌接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值