springboot2整合swagger简单入门

gdjlc      2022-04-22     300

关键词:

Swagger是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。

1、pom.xml添加配置

可以到http://mvnrepository.com上搜索springfox,便可以看到Springfox Swagger2和Springfox Swagger Ui的版本。

    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

2、swagger的configuration

package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.annotations.ApiOperation;
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
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //.apis(RequestHandlerSelectors.basePackage("com.example.demo.web"))//包所在位置
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any()).build().useDefaultResponseMessages(false);
    }

    /**
     * 获取swagger ApiInfo
     *
     * @return
     */
    private static ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API文档")
                .description("Swagger API 文档")
                .termsOfServiceUrl("http://www.cnblogs.com/gdjlc")
                .version("1.0")
                .contact(new Contact("name..", "url..", "email.."))
                .build();
    }
}

3、Controller中测试方法

package com.example.demo.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@Api(value = "UserController", tags = "用户操作相关接口")
public class UserController {
                
    @ResponseBody    
    @GetMapping("/helloWorld")
    @ApiOperation(value="测试value", notes = "测试notes")
    @ApiImplicitParam(paramType="query", name = "userName", value = "用户编号", required = true, dataType = 

"String")
    public String helloWorld(@RequestParam String userName){
        return "hello world," + userName;
    }
}

Swagger使用的注解及其说明:
@Api:用在类上,说明该类的作用。
@ApiOperation:注解来给API增加方法说明。
@ApiImplicitParams : 用在方法上包含一组参数说明。
@ApiImplicitParam:用来注解来给方法入参增加说明。
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
@ApiModel:描述一个Model的信息

 

访问:项目地址/swagger-ui.html,如我的http://localhost:9001/swagger-ui.html
结果如下,可以点击方法右边的【Try it out】按钮开始测试,之后输入参数,点击【Execute】执行查看结果。

springboot入门二十九,整合swagger3

1.pom.xml添加引用 引入knife4j是为了更好的支持Swagger3,不想引入也可以<!--3.引入Swagger3依赖--><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId>& 查看详情

springboot2.0整合freemarker快速入门

目录1.快速入门1.1创建工程pom.xml文件如下1.2编辑application.yml1.3创建模型类1.4创建模板1.5创建controller1.6测试2.FreeMarker基础2.1数据模型2.2List指令2.3遍历Map数据2.4if指令2.5运算符2.6空值处理2.7内建函数freemarker是一个用Java开发的模板引... 查看详情

springboot2整合knife4j

knife4j官网: https://doc.xiaominfo.com/guide/useful.html#java%E5%BC%80%E5%8F%91这玩艺就swagger的升级版,但是用起来比swagger方便多了,至少不会出现莫名的版本兼容问题下面记录一个配置示例 1. 代码结构 2.pom.xml<?xmlversion="1.0"enc... 查看详情

springbootspringboot与swagger整合(三十)

  本章介绍SpringBoot与Swagger整合,对Swagger不了解的,可以参考【Java】Swagger快速入门Swagger整合  1、新建一个SpringBootWeb项目,引入swagger依赖,如下:1<!--swagger-->2<dependency>3<groupId>io.springfox</groupId>4<ar 查看详情

springboot学习入门简易版五---springboot2.0整合jsp(11)(代码片段)

springboot对jsp支持不友好,内部tomcat对jsp不支持,需要使用外部tomcat,且必须打包为war包。 1创建maven项目 注意:必须为war类型,否则找不到页面。且不要把jsp页面存放在resources(原因:可能被别人访问,其次不在classes类... 查看详情

springspringboot2.6.4整合swagger3.0.0填坑

【Spring】SpringBoot2.6.4整合Swagger3.0.0填坑仓库配置使用如下依赖<!--swagger-ui--><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><vers 查看详情

springboot学习入门简易版一---springboot2.0介绍

1.1为什么用springboot(2)传统项目,整合ssm或ssh,配置文件,jar冲突,整合麻烦。Tomcat容器加载web.xml配置内容springboot完全采用注解化(使用注解方式启动springmvc,没有web.xml,springmvc3后采用注解方式启动springmvc),简化配置,快速整... 查看详情

springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层

2.4.4SpringBoot静态资源访问(9)Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则/static /public /resources /META-INF/resources可以在src/main/resources目录下创建static,在该位置放置一个图片文件。启动... 查看详情

springboot2.0之四简单整合mybatis

  从最开始的SSH(Struts+Spring+Hibernate),到后来的SMM(SpringMVC+Spring+MyBatis),到目前的S(SpringBoot),随着框架的不断更新换代,也为我们广大的程序猿提供了更多的方便,一起搭建一个从控制层到持久层的项目可能需要一两... 查看详情

springboot学习入门简易版八---springboot2.0多环境配置整合mybatismysql8+(19-20)(代码片段)

2.11SpringBoot多环境配置(19) application.properties中配置Spring.profiles.active=prd配置环境:Application-dev.properties开发环境Application-test.properties测试环境Application-uat.properties用户测试环境Application-prd.proper 查看详情

springboot2.2.5整合thymeleaf模版引擎,并实现简单的页面操作

参考技术A前言:该博客主要是记录自己学习的过程,方便以后查看,当然也希望能够帮到大家。后记:本次分享到此结束,本人水平有限,难免有错误或遗漏之处,望大家指正和谅解,欢迎评论留言。 查看详情

springcloud整合swagger统一服务api(代码片段)

Swagger整合多个微服务统一API接口文档单系统SpringBoot整合Swagger或者Swagger注解介绍 请看另一篇文章Swagger(丝袜哥)快速入门(超详细介绍)准备工具1. 自己搭建准备好微服务环境2.里面至少包含一个注册中心,一个网关... 查看详情

springboot2.x:swagger2的正确玩儿法

Swagger2简介简单的来说,Swagger2的诞生就是为了解决前后端开发人员进行交流的时候API文档难以维护的痛点,它可以和我们的Java程序完美的结合在一起,并且可以与我们的另一开发利器SpringBoot来配合使用。开始使用第一步:导入... 查看详情

springboot2整合nacos组件,环境搭建和入门案例详解(代码片段)

摘自:https://www.cnblogs.com/cicada-smile/p/12190192.html 本文源码:GitHub·点这里 || GitEE·点这里一、Nacos基础简介1、概念简介Nacos是构建以“服务”为中心的现代应用架构,如微服务范式、云原生范式等服务基... 查看详情

springboot2.x版本配置swagger2

参考技术A为什么要引入Swagger?有过后台开发和前端联调的人都会被接口文档折磨,更新不及时,文档和代码不一致,无法调试,swagger就是为了解决这个问题。看下swagger官方介绍Swagger是一个规范和完整的框架,用于生成、描述、... 查看详情

springboot整合swagger测试api构建

@Author:SimpleWu什么是Swagger?Swagger是什么:THEWORLD’SMOSTPOPULARAPITOOLING根据官网的介绍:SwaggerInspector:测试API和生成OpenAPI的开发工具。SwaggerInspector的建立是为了解决开发者的三个主要目标。执行简单的API测试生成OpenAPI文档探索新... 查看详情

swagger简单快速入门教程(代码片段)

一、使用springfox生成接口UI1、导入springfox-boot-starter依赖<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version& 查看详情

springboot集成swagger2生成api接口文档(代码片段)

SpringBoot2.3.0集成Swagger2引入Swagger2相应的依赖入门示例SpringBoot2集成Swagger2后启动报错结语背景:最近在工作中发现,已经多次发现后台开发人员提供的接口协议和实际的业务代码不统一。这些现象往往都是因为开发人员在... 查看详情