• springboot中swaggerUI的使用


    该文章已更新,请查看

    http://www.cnblogs.com/yzlpersonal/p/6420507.html

     

    demo地址:demo-swagger-springboot

    springboot中swaggerUI的使用

    1.pom文件中添加swagger依赖

    2.从github项目中下载swaggerUI

    然后把dist目录下的所有文件复制到springboot项目的webapp下面,修改index.html中的: url = "http://petstore.swagger.io/v2/swagger.json";为---------> url="http://localhost:8080/api-docs";

    3.spring boot启动类

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

       /** 配置 swagger开始*/ private SpringSwaggerConfig springSwaggerConfig;

    /**
     * Required to autowire SpringSwaggerConfig
     */
    @Autowired
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
        this.springSwaggerConfig = springSwaggerConfig;
    }
    
    
    /**
     * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
     * framework - allowing for multiple swagger groups i.e. same code base
     * multiple swagger resource listings.
     */
    @Bean
    public SwaggerSpringMvcPlugin customImplementation() {
        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns(".*?");
    }
    
    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo(
                "测试接口文档(标题)", // App Service API
                "这是一个描述信息。。。",
                "termsOfServiceUrl",
                "younaame@yourmail.com",
                "2222222222222222222222222222",
                "3333333333333333333333333333");
        return apiInfo;
    }
    

      /** 配置 swagger结束*/

    4. 添加测试rest接口类:

    @RestController
    public class HelloController {
    
        @RequestMapping(value = "/hello")
        @ApiOperation(value = "添加用户",httpMethod ="POST", response = User.class,notes = "HelloWorld")
        public User hello(@ApiParam(required = true,name = "paramData",value = "用户信息 json 数据") String paramData){
            User userBean = new User();
    

               userBean.setName("测试用户");            userBean.setOtherInfo("其他信息"); return userBean; } }

    5 访问http://localhost:8080/api-docs,返回:

    {
    "apiVersion": "1.0",
    "apis": [
      {
        "description": "Basic Error Controller",
        "path": "/default/basic-error-controller",
        "position": 0
      },
      {
        "description": "Hello Controller",
        "path": "/default/hello-controller",
        "position": 0
      }
    ],
    "authorizations": {
    
    },
    "info": {
      "contact": "younaame@yourmail.com",
      "description": "这是一个描述信息。。。",
      "license": "2222222222222222222222222222",
      "licenseUrl": "3333333333333333333333333333",
      "termsOfServiceUrl": "termsOfServiceUrl",
      "title": "测试接口文档(标题)"
    },
    "swaggerVersion": "1.2"
    

    }  说明配置正确。  然后再访问:http://localhost:8080/swagger/index.html,查看restful风格接口信息和效果。

    逃避不一定躲得过,面对不一定最难过
  • 相关阅读:
    [读书笔记]捉虫日记
    花生壳建站全过程
    (step4.3.9)hdu 1584(蜘蛛牌——DFS)
    鼠标移到导航上面 当前的LI变色 处于当前的位置
    JavaScript学习笔记
    由Maximum Gap,对话桶排序,基数排序和统计排序
    安德鲁斯Launcher得到的装在手机的应用程序列表
    broadAnywhere:Broadcast组件权限绕过漏洞(Bug: 17356824)
    Ubuntu logomaker sh: 1: pngtopnm: not found 解决方案
    HDU 1598 find the most comfortable road (罗列+Kruskal) 并检查集合
  • 原文地址:https://www.cnblogs.com/yangzhenlong/p/6113593.html
Copyright © 2020-2023  润新知