其他分享
首页 > 其他分享> > springBoot中的swagge配置及使用

springBoot中的swagge配置及使用

作者:互联网

1、从maven仓库中导入依赖:

<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>

 

2、@EnableSwagger2 注解添加到对应的运行环境中,启用Swagger2,代码如下:

@SpringBootApplication
@EnableSwagger2
public class SpringSwaggerApplication {

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

}

 

3、在SwaggerConfig类中配置Swagger,代码如下:

package com.liu.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 启用swagger2 @EnableSwagger2 public class SwaggerConfig {
//分组 @Bean public Docket docket1(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("A"); } @Bean public Docket docket2(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("B"); } @Bean public Docket docket3(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("C"); } @Bean public Docket docket(Environment environment){//传入environment环境 // 设置要显示swagger的环境 是否在dev环境中 Profiles profiles = Profiles.of("dev"); // 判断自己是否在当前环境中 boolean flag = environment.acceptsProfiles(profiles); //     System.out.println(flag);
//     创建Docket对象 return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .groupName("刘") // 是否启用swagger .enable(flag) .select() //配置扫描的方式basePackage // withMethodAnnotation // withClassAnnotation .apis(RequestHandlerSelectors.basePackage("com.liu.swagger.controller")) // 设置过滤路径 //.paths(PathSelectors.ant("")) .build(); }
//        设置相关信息 private ApiInfo apiInfo(){
//        创建一个Contact对象 Contact contact = new Contact( "刘浩然", "https://liu.com", "1216688798@qq.com"); return new ApiInfo( "liu的swaggerAPI日记", "我爱Java", "1.0", "https://liu.com", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList() ); } }

 

4、.properties文件

application.properties文件中:spring.profiles.active=dev

application-dev.properties:server.port=8081

application-pro.properties:server.port=8082

 

5、测试swagger的链接:http://localhost:8081/swagger-ui.html

 

6、插入注解

package com.liu.swagger.pojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
//@Api
@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}

 

7、Controller层

package com.liu.swagger.controller;

import com.liu.swagger.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {

    @ApiOperation("hello控制")
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    @PostMapping ("/user")
    public User user(){
        return new User();
    }
    @PostMapping("/hello")
    public String hello1(@ApiParam("用户名") String username){
        return "hello"+username;
    }
}

 

8、测试页面:

 

标签:springfox,springBoot,Docket,配置,org,import,swagger,swagge,public
来源: https://www.cnblogs.com/lhr123/p/16444287.html