SpringBoot 教程 | 第一篇: SpringBoot项目整合Swagger2(Spring

作者:神秘网友 发布时间:2020-10-23 23:14:37

SpringBoot 教程 | 第一篇: SpringBoot项目整合Swagger2(Spring

SpringBoot 教程 | 第一篇: SpringBoot项目整合Swagger2(SpringBoot+Swagger2)(2.6.1版本)

一、项目添加Swagger pom依赖

<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>

二、项目主启动类 添加 Swagger2 注解

@EnableSwagger2
public class CustomerApplication {
    public static void main(String[] args) {
        SpringApplication.run(CustomerApplication.class, args);
    }
 }

三、新建 Swagger2 配置类 与主启动类同级 结构如下图:
SpringBoot 教程 | 第一篇: SpringBoot项目整合Swagger2(Spring
3.1 Swagger2 配置类 代码如下:

package com.customer;

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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class Swagger2 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.customer.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot利用swagger构建api文档")
                .description("简单优雅的restfun风格")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }
    
}

四、新建测试类 SwaggerTestController 测试代码如下:

package com.customer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(description = "Swagger测试类")
public class SwaggerTestController {

    @ApiOperation(value = "getSwaggerTest-请求测试")
    @GetMapping(value = "getSwaggerTest")
    public String getSwaggerTest(@RequestParam("name") String name){
        System.out.println("getSwaggerTest:" + name);
        return "getSwaggerTest:" + name;
    }
    
}

五、启动Eureka注册中心8900服务 和 此项目8902 服务,
访问Eureka注册中心 http://localhost:8900 如下所示:

SpringBoot 教程 | 第一篇: SpringBoot项目整合Swagger2(Spring结论:服务成功注册到Eureka注册中心。

5.1 浏览器开始访问 http://localhost:8902/swagger-ui.html 如下图:
SpringBoot 教程 | 第一篇: SpringBoot项目整合Swagger2(Spring开始进行Swagger 测试验证:
SpringBoot 教程 | 第一篇: SpringBoot项目整合Swagger2(Spring

SpringBoot 教程 | 第一篇: SpringBoot项目整合Swagger2(Spring结论:Swagger进行测试和查看控制台输出分析-均有返回值信息(getSwaggerTest:测试)-验证成功。

5.2 此时此刻 SpringBoot项目整合Swagger2 就成功了。

SpringBoot 教程 | 第一篇: SpringBoot项目整合Swagger2(Spring相关教程

  1. Springboot 同时支持Http和Https访问

    Springboot 同时支持Http和Https访问 1、配置文件 #端口配置server: port: 443 tomcat: max-threads: 200 uri-encoding: UTF-8 ssl: enabled: true key-store: classpath:XX.XX.com.jks key-password: XXX key-store-type: JKShttp: port: 8098 证书直接放在r

  2. SpringBoot集成swagger-OpenAPI

    SpringBoot集成swagger-OpenAPI OpenAPI是swagger下的一个在线查看接口文档的工具包,可以随着项目的部署同时生成项目对应的所有接口文档 一、在pom引入maven的jar包 !--OpenApi-- dependency groupIdorg.springdoc/groupId artifactIdspringdoc-openapi-ui/a

  3. springboot整合swagger小栗子

    springboot整合swagger小栗子 ?xml version=1.0 encoding=UTF-8?project xmlns=http://maven.apache.org/POM/4.0.0 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xs

  4. SpringBoot中使用Thymeleaf

    SpringBoot中使用Thymeleaf !-- thymeleaf会对html中的标签进行严格校验,如果html标签缺少结束标签的话,thymeleaf会报错,我们可以通过下面方式去除thymeleaf的校验 -- dependency groupIdnet.sourceforge.nekohtml/groupId artifactIdnekohtml/artifactId

  5. VSCode开发UNI-APP 配置教程、注意事项与插件推荐

    VSCode开发UNI-APP 配置教程、注意事项与插件推荐 VSCode开发UNI-APP 配置教程、注意事项与插件推荐 uni-app 是一个使用 Vue.js 开发所有前端应用的框架,开发者编写一套代码,可发布到iOS、Android、H5、以及各种小程序(微信/支付宝/百度/头条/QQ/钉钉/淘宝

  6. springBoot 整合UEditor百度富文本编辑器。

    springBoot 整合UEditor百度富文本编辑器。 UEditor是由百度web前端研发部开发所见即所得富文本web编辑器。可以定制很多如新闻页面,博客等等。UEditor使用简单,支持二次开发,适合快速开发很多业务功能,研究几天发个学习文档。 UEditor分为前端和后端前端

  7. springboot引入com.baidubce:bce-java-sdk百度的SDK导致JSP页面

    springboot引入com.baidubce:bce-java-sdk百度的SDK导致JSP页面的标签无法正常使用报cannot be resolved in either web.xml or 项目是使用Springboot 构建的spring+springMVC+mybatis 程序,后台管理页面使用的是JSP作为显示层。在jsp页面中使用了各种标签。

  8. springboot 系统事件监听

    springboot 系统事件监听 springboot的事件在org.springframework.boot.context.event包下,可通过GenericApplicationListener监听。 springboot的LoggingApplicationListener集成此接口实现。 可以参考此设计来实现我们自己的一些系统配置的自定义业务。 事