spring boot-2.1.16整合swagger-2.9.2 含yml配置文件

java代码

package com.oauth.util;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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;

@Configuration
@EnableSwagger2
//是否开启swagger
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Swagger2 {

	// swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				// 为当前包路径
				.apis(RequestHandlerSelectors.basePackage("com.oauth.controller")).paths(PathSelectors.any()).build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				// 页面标题
				.title("Swagger2")
				// 创建人信息
				.contact(new Contact("scy", "666", "888"))
				// 版本号
				.version("1.0")
				// 描述
				.description("API 描述").build();
	}
}

yml文件

server:
  port: 8587

spring:
  application:
    name: auth
    
eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8090/eureka/
      
swagger:
  enable: true

swagger:
enable: true 这里是设置是否启动 本地和测试环境为true 正式环境为false

controller

package com.oauth.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("api")
@Api(value = "测试接口", tags = "IndexController")
public class IndexController {

	@ApiOperation(value = "hello")
	@GetMapping("hello")
	public String hello() {
		return "Hello World";
	}

	@ApiOperation(value = "hello2")
	@GetMapping("api/hello")
	public String apiHello() {
		return "Hello World";
	}

}

打开swagger页面 localhost:端口号/swagger-ui.html
在这里插入图片描述

如果swagger:
enable: false 这里设置为false
在这里插入图片描述

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用 Micronaut 框架与 Swagger 集成来生成和展示 API 文档。下面是使用 Micronaut 和 Swagger 的步骤: 1. 添加依赖:在你的构建工具(如 Maven 或 Gradle)中添加 Micronaut Swagger 的依赖。例如,对于 Maven,可以在 `pom.xml` 文件中添加以下依赖: ```xml <dependency> <groupId>io.micronaut.configuration</groupId> <artifactId>micronaut-openapi</artifactId> <scope>compile</scope> </dependency> ``` 2. 配置 Swagger:在 `application.yml` 或 `application.properties` 文件中添加以下配置: ```yaml micronaut: router: static-resources: swagger: paths: classpath:META-INF/swagger mapping: /swagger/** ``` 3. 编写 API 控制器:创建一个 Micronaut 控制器类,使用 `@Controller` 注解标记,同时使用 `@Api` 和 `@ApiOperation` 注解来定义接口和操作。例如: ```java import io.micronaut.http.annotation.Controller; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @Controller("/api") @Api("/api") @Tag(name = "Hello") public class HelloController { @Get("/hello") @Operation(summary = "Say hello") public String hello() { return "Hello, Micronaut!"; } } ``` 4. 运行应用程序:启动你的 Micronaut 应用程序,可以使用 Gradle 或 Maven 命令运行。例如,对于 Gradle,可以使用以下命令: ``` ./gradlew run ``` 5. 查看 Swagger 文档:在浏览器中访问 `http://localhost:8080/swagger`,你将能够看到自动生成的 Swagger 文档,其中包了你的 API 接口和操作的详细描述、参数和响应模型等信息。 通过上面的步骤,你就可以在 Micronaut 应用程序中集成 Swagger,并通过 Swagger 自动生成和展示 API 文档了。希望对你有所帮助!如果你有任何进一步的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值