首页 > 其他分享 >SpringBoot项目引入Swagger接口文档

SpringBoot项目引入Swagger接口文档

时间:2022-09-01 11:12:27浏览次数:52  
标签:springfox SpringBoot Swagger documentation springframework book 文档 import swagge

一、在项目中引入Swagger依赖

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

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

二、Swagger配置

  1、新建两个类,分别为SwaggerProperties  SwaggerConfiguration

  

  2.SwaggerProperties类的内容

package com.example.book.config.swagger.properties;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "swagger")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SwaggerProperties {
    
    //标题
    private String title;
    //描述
    private String description;
    //版本
    private String version;
    //作者
    private String author;
}

3.SwaggerConfiguration配置内容

package com.example.book.config.swagger;


import com.example.book.config.swagger.properties.SwaggerProperties;
import io.swagger.annotations.ApiOperation;
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;

import javax.annotation.Resource;

@Configuration
@EnableSwagger2
public class SwaggerConfiguration{

    @Resource
    private SwaggerProperties swaggerProperties;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("接口文档")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                //是否使用默认响应消息
                .useDefaultResponseMessages(false);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription())
                .contact(new Contact(swaggerProperties.getAuthor(), "", ""))
                .version(swaggerProperties.getVersion())
                .build();
    }

}

代码的一些解释如下

 

 4.在application.yml中配置title,等

swagger:
  title: "项目相关服务说明文档"
  description: "作者很懒,什么也没下"
  version: "1.0"
  author: "thd"

三、在Controller中测试Swagger能否正常使用,写一个接口

package com.example.book.controller;


import com.example.book.service.IBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.awt.print.Book;
import java.util.List;

@RestController
@Api(tags = {"图书管理控制器"})
@RequestMapping("/book")
@Slf4j
public class BookController {

    @Resource
    IBookService bookService;

    @GetMapping("/getAll")
    @ApiOperation(value = "获取所有-book",notes = "获取所有-book")
    public List<Book> getAll(){
        return bookService.getAll();
    }


}

 

 

 五、访问http://localhost:4399/doc.html,此地址为Swagger自动生成,4399为自己设置的server.port=4399

  

 

 完成!

标签:springfox,SpringBoot,Swagger,documentation,springframework,book,文档,import,swagge
From: https://www.cnblogs.com/tianhuida/p/16645805.html

相关文章

  • springboot+Vue项目允许跨域
    packagecom.example.demo.itkb.user.config;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotat......
  • SpringBoot简单使用(1)
    1:xml文件与javaconfig 1.1.1什么是javaconfig:是spring提供的使用java类配置容器。配置springIOC容器的纯java方法优点:可以使用面向对象的方式,一个配置类可以继承配置......
  • 轻量级SpringBoot Office文档在线预览框架
    框架简介介绍:基于开源项目KkFileView源码提取出,封装成仅用于Office文档预览(格式转换)功能的一个通用组件;原理是把Word转成PDF,PPT转成PDF,Excel转成HTML;利用浏览......
  • Linux基础知识(14)- Docker (七) | 使用 Docker 部署 SpringBoot 项目
    本文将完全复制“Springboot基础知识(08)-spring-boot-starter-web(Web启动器)”里的SpringbootWeb项目的代码和配置到新项目SpringbootWebDocker。在新项目Springboot......
  • SpringBoot使用@Async和@Transactional注解优化接口
    1、业务背景:项目上有一个接口需要按照前端传递的时间段范围修改6个表的数据,接口V1版本开发完成是使用的同步方式全局@Transactional注解的方式去做的,但存在一个问题就......
  • SpringBoot整合Shiro
    11、SpringBoot整合Shiro11.1、什么是ShiroApacheShiro是一个Java的安全(权限)框架。Shiro可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE环境,也可以用在Jav......
  • Neo4j在linux上的安装与Springboot的集成
    Neo4j在linux上的安装与Springboot的集成在linux安装:前提:安装配置好java环境1.下载neo4j官方社区版下载地址:https://neo4j.com/download-center/#releases或直接使......
  • SpringBoot slighting matters(3)
    pom版本管理<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.0.RELEASE</versio......
  • springBoot中新建拦截器
    最近项目中需要将请求中的header中增加几个字段,第一反应就是可不可以用拦截器拦截请求后将字段放进请求头中,经过网上一番学习后发现该方案可行,故将此方法记录下来供大家参......
  • 【SpringBoot】springProject‘org.springframework.boot:spring-boot-starter-parent
    IDEA中搭建Spring体系,maven子项目引入父项目,子项目的pom文件和maven插件中会提示红色错误:  EA默认会缓存Maven本地库中的依赖项,导致引入的依赖版本在仓库中没找到。......