swagger-ui

先来个灵魂三问,其实很多的东西在实际的项目中使用,必须明白这个是否是合适我们现在的项目实现.

swagger是一个生成一个在线api文档,因为当下很多的前后端分离的项目,做这个接口调试的时候,很需要这个东西,swagger通过利用 springfox.documentation ,通过注入bean的方式,便捷的可以生成这样的文档..话不多说,上代码,看看具体怎么用

1.pom依赖

<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.配置文件

package com.mall_learning;

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 //启用swagger2
public class Swagger2Config {

    @Bean
    public Docket getSwaggerApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(getApiInfo())
                .select()
                //所有该包下面的文件都进行生成api文档
                .apis(RequestHandlerSelectors.basePackage("com.mall_learning"))
                //为有@Api注解的Controller生成API文档
//                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //为有@ApiOperation注解的方法生成API文档
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo getApiInfo(){
        return  new ApiInfoBuilder().title("SwaggerUI演示")
                .description("mall")
                .contact("xueshuai")
                .version("1.0")
                .build();

    }
}

3.controller,方法上,参数上,具体的实体类中的字段增加注解

package com.mall_learning.controller;


import com.mall_learning.model.PmsBrand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/hello")
@Api(tags="Hello",description = "你好测试")
public class Hello {
    @RequestMapping(value = "/sayHello",method = RequestMethod.POST)
    @ApiOperation("方法")
    @ResponseBody
    public String sayHello( PmsBrand PmsBrand) {
        return "你好" + PmsBrand.toString();
    }
    @RequestMapping(value = "/getByid{id}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("通过id获取结果值")
    public String getById(String id){
        return  "id"+id;
    }
}

4.实现效果http://localhost:8080/swagger-ui.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值