欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot-2.1.16整合swagger-2.9.2 含yml配置文件的代码详解

程序员文章站 2022-04-06 14:11:40
java代码package com.oauth.util;import org.springframework.boot.autoconfigure.condition.conditionalonpr...

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

spring boot-2.1.16整合swagger-2.9.2 含yml配置文件的代码详解

如果swagger:
enable: false 这里设置为false

spring boot-2.1.16整合swagger-2.9.2 含yml配置文件的代码详解

总结

到此这篇关于spring boot-2.1.16整合swagger-2.9.2 含yml配置文件的文章就介绍到这了,更多相关spring boot整合swagger内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!