前后端分离

导入所需要的swagger包

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

配置它。

package com.swagger.swagger.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {




//    配置了swagger的docket实例
    @Bean
    public Docket docket(Environment environment){
      //确定环境
        Profiles profiles = Profiles.of("dev", "test");
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)//enable表示是否启动swagger,若为false则swagger不会在浏览器中访问
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.swagger.swagger.controller"))
                .build()
                ;
    }

    //配置swagger信息   apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("ZCODEX", "http://iink.vip/", "1226776310@qq.com");
        return new ApiInfo(
               "ZCODEX API",
                "这是一个API描述",
                "v1.0",
                "http://iink.vip/",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()

        );
    }
}

写controller,其实没啥用就是测试一下

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String Hello(){
        return "hello";
    }
}

http://localhost:8081/swagger-ui.html

我们打开这个网址就能够看见swagger页面

配置api分组

.groupName("ZCODEX")

配置多个分组,(适合团队开发)

 @Bean
    public Docket docket1(){
      return new Docket(DocumentationType.SWAGGER_2).groupName("BB");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("AA");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("CC");
    }

配置一个实体类,这里的注解意思是在前端显示的注释

@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}

控制层,可以在前端看见

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String Hello(){
        return "hello";
    }
    //只要我们的接口中返回值存在实体类就会被扫描到swagger中
    @PostMapping("/user")
    public User user(){
        return new User();
    }
    @ApiOperation("hello控制类")
    @GetMapping("/user2")
    public String user2(@ApiParam("用户名") String username){
        return "hello"+username;
    }
    @ApiOperation("POST测试")
    @PostMapping("/post")
    public User user3(User user){
        return user;
    }

}

总结:

  1. 我们可以给swagger给一些比较难理解的属性或者接口。增加注释信息
  2. 接口文档实时更新
  3. 可以在线测试

这里使用swagger代替了postman,最后别忘了在pro环境 中关闭swagger