添加swagger

先后端分离开发时,前端如何得知后端的接口名称以及参数,后端如何调试本身的端口是否正确是一个很严肃的话题,swagger为这些问题提供了很大的便利html

一、在pom文件加依赖前端

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.9.2</version>
</dependency>
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.9.2</version>
</dependency>java

二、新建一个 java 类 spring

@Bean
    public Docket buildDocket() {
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//调用下面apiInfo()方法
                .select()
                //Controller所在路径
                .apis(RequestHandlerSelectors.basePackage("com.unicom.news.controller"))  // controller 所在包名
                .paths(PathSelectors.any())
                .build();
      }
 
    public ApiInfo apiInfo() {
            return  new ApiInfoBuilder()
                    .title("springboot结合swagger2构建Restful API")
                    .description("这是一个swagger2小型demo")
                    .termsOfServiceUrl("localhost:8080")
                    .version("0.0.1")
                    .build();
 
     }后端

三、启动项目后,打开浏览器,在浏览器输入http://localhost:8080/swagger-ui.html 便可查看端口,以及端口的method,参数api

/**********************************   转载请附上连接 https://www.cnblogs.com/renxq/p/11075272.html ***********************************/浏览器

相关文章
相关标签/搜索