学习笔记:spring-boot整合Swigger UI

1.在pom.xml中添加依赖
 <!--Swagger-UI API文档生产工具-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
2.创建配置类
/**
 * @author vetstein
 * @creat 2021-08-2021/8/28-13:00
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                        .apis(RequestHandlerSelectors.basePackage("com.company.mall.controller"))
                        .paths(PathSelectors.any())
                        .build();
        //为有@Api注解的Controller生成API文档
//                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
        //为有@ApiOperation注解的方法生成API文档
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SwaggerUI")
                .description("mall项目")
                .contact(new Contact("verstein","http://localhost:8080","163.com"))
                .version("1.0")
                .build();
    }
}
3.配置接口方法

@API作用在类上
@ApiOperation 定义在方法上,描述方法名、方法解释、返回信息、标记等信息。
@ApiImplicitParams 用于描述方法的返回信息,和 @ApiImplicitParam 注解配合使用;
@ApiImplicitParam 用来描述具体某一个参数的信息,包括参数的名称、类型、限制等信息。
@ApiModel 用在返回对象类上
@ApiModelProperty 用在出入参数对象的字段上

/**
 * @author vetstein
 * @creat 2021-08-2021/8/28-11:06
 */
@Api(tags = "PmaBrandController",description ="商品品牌管理" )
@Controller
@RequestMapping("/brand")
public class PmsBrandController {

    @Autowired
    PmsBrangServiceImpl pmsBrandService;

    @ApiOperation("hello world!")
    @ResponseBody
    @GetMapping("/hello")
    public String HelloWorld() {
        return "Hello World!";
    }

    @ApiOperation("获取所有品牌")
    @GetMapping("listAll")
    @ResponseBody
    public List<PmsBrand> getBrangList() {
       return pmsBrandService.listAllBrand();
    }

    @ApiOperation("增加品牌")
    @ResponseBody
    @PostMapping("/create")
    public int createBrand(@RequestBody PmsBrand brand) {
        int p= pmsBrandService.creatBrand(brand);
        return p;
    }

    @ApiOperation("修改品牌")
    @ResponseBody
    @PostMapping("update/{id}")
    public int updateBrand(@PathVariable Long id, @RequestBody PmsBrand brand) {
        int i = pmsBrandService.updateBrand(id, brand);
        return i;
    }

    @ApiOperation("删除品牌")
    @ResponseBody
    @GetMapping("delete/{id}")
    public int deleteBrand(@PathVariable Long id) {
        int i = pmsBrandService.deleteBrand(id);
        return i;
    }

    @ApiOperation("根据id查询品牌")
    @ResponseBody
    @GetMapping("/{id}")
    public PmsBrand getBrand(@PathVariable Long id) {
        PmsBrand brand = pmsBrandService.getBrand(id);
        return brand;
    }

}
4.访问地址

访问地址:http://项目实际地址/swagger-ui.html

posted @ 2021-08-28 14:24  夏天的风key  阅读(105)  评论(0编辑  收藏  举报