5分钟快速在SpringCloud中添加Swagger

本文介绍了如何在SpringCloud项目中快速集成Swagger,包括添加依赖、配置应用yaml文件、设置接口解析以及启动后如何访问 Swagger UI。适用于使用 Maven 3.5、JDK 8+、Spring-Boot 1.5.3 和 Spring-Cloud Dalston.SR1 的开发环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringCloud添加Swagger

目录:

一.基本介绍

官方介绍:Swagger是一个规范且完整的框架,提供描述、生产、消费和可视化RESTful Web Service。
专业角度:Swagger是由庞大工具集合支撑的形式化规范。这个集合涵盖了从终端用户接口、底层代码库到商业API管理的方方面面。
##二.如何使用


开发环境

  • maven 3.5
  • jdk 8+
  • spring-boot 1.5.3
  • spring-cloud Dalston.SR1
  • fastjson 1.2.41

添加依赖

 <!-- Swagger核心包 start -->
 <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.6.1</version>
 </dependency>
 <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
 </dependency>
<!-- Swagger核心包 end -->

application.yml中添加

# 配置eureka获取服务地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1001/eureka/
  # 配置Swagger相关信息
  instance:
      prefer-ip-address: true
      instanceId: ${
   
   spring.cloud.client.ipAddress}:${
   
   server.port}
      status-page-url: 
	  http://${
   
   spring.cloud.client.ipAddress}:${
   
   server.port}/swagger-ui.html 
      # ${
   
   server.port}为该服务的端口号

添加配置

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
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值