ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springBoot中的swagge配置及使用

2022-07-04 20:36:46  阅读:136  来源: 互联网

标签:springfox springBoot Docket 配置 org import swagger swagge public


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有