Bladex生成Swagger的方法

举报
穆雄雄 发表于 2022/12/07 21:52:52 2022/12/07
【摘要】 一、在启动类中添加如下代码:(目的是为了打印输出swagger的地址等)注解:@Slf4j实现接口:CommandLineRunner依赖注入:@Autowired private Environment environment;@Override public void run(String... strings) throws Exception { try { String po...

一、在启动类中添加如下代码:(目的是为了打印输出swagger的地址等)
注解:@Slf4j
实现接口:CommandLineRunner
依赖注入:

@Autowired
	private Environment environment;
@Override
	public void run(String... strings) throws Exception {
		try {
			String port = Optional.ofNullable(environment.getProperty("server.port")).orElse("8080");
			log.info("\n------------------------环境信息---------------------------\n\t" +
					"Application '{}' is running! Access URLs:\n\t" +
					"Local  : \thttp://{}:{}\n\t" +
					"Swagger: \thttp://{}:{}/doc.html\n\t" +
					"Profile(s): \t{}\n----------------------------------------------------------",
				environment.getProperty("spring.application.name"),
				InetAddress.getLocalHost().getHostAddress(),
				port,
				InetAddress.getLocalHost().getHostAddress(),
				port,
				Arrays.toString(environment.getActiveProfiles()));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

启动类全部代码如下:

/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade;

import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.launch.BladeApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Optional;

/**
 * 启动器
 *
 * @author Chill
 */
@Slf4j
@EnableScheduling
@SpringBootApplication
public class Application  implements CommandLineRunner {
	@Autowired
	private Environment environment;

	@Bean
	public RestTemplate restTemplate(RestTemplateBuilder builder){
		return builder.build();
	}

	public static void main(String[] args) {
		BladeApplication.run(CommonConstant.APPLICATION_NAME, Application.class, args);
	}

	@Override
	public void run(String... strings) throws Exception {
		try {
			String port = Optional.ofNullable(environment.getProperty("server.port")).orElse("8080");
			log.info("\n------------------------环境信息---------------------------\n\t" +
					"Application '{}' is running! Access URLs:\n\t" +
					"Local  : \thttp://{}:{}\n\t" +
					"Swagger: \thttp://{}:{}/doc.html\n\t" +
					"Profile(s): \t{}\n----------------------------------------------------------",
				environment.getProperty("spring.application.name"),
				InetAddress.getLocalHost().getHostAddress(),
				port,
				InetAddress.getLocalHost().getHostAddress(),
				port,
				Arrays.toString(environment.getActiveProfiles()));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

}


二、找到需要生成Swagger接口文档的控制器类(eg:ActiveCodeController):
1.在控制器类的上面添加注解:
@Api(value = "卡信息", tags = "卡信息接口")

value里面写的是:这个控制器的描述,或者功能

如图所示:
在这里插入图片描述

2.在需要生成文档的方法上写注解:
@ApiOperation(value = "详情", notes = "传入cardInfo")
value:接口的功能或者描述
notes:传入参数的描述
全部代码:

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入cardInfo")
	public R<CardInfo> detail(CardInfo cardInfo) {
		CardInfo detail = cardInfoService.getOne(Condition.getQueryWrapper(cardInfo));
		return R.data(detail);
	}

或者:

	/**
	 * 分页 卡信息
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入cardInfo")
	public R<IPage<CardInfo>> list(CardInfo cardInfo, Query query) {
		IPage<CardInfo> pages = cardInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(cardInfo));
		return R.data(pages);
	}

其他的可以自己补充。
三、配置Swagger的配置类,类路径:BladeX-Boot/src/main/java/org/springblade/common/config/SwaggerConfiguration.java

加入如下代码:

	@Bean
	public Docket developerDocket() {
		return docket("开发者中心接口", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.developer"));
	}

【开发者接口】:就是Swagger的接口功能的名称
【.modules.developer】:让Swagger扫描哪个包,指向包就可以。

然后启动服务,启动完成之后,查看控制台打印的Swagger地址。
在这里插入图片描述
点击地址就可以查看

在这里插入图片描述

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。