首页 > 其他分享 >swagger配置类

swagger配置类

时间:2023-03-22 12:14:16浏览次数:38  
标签:swagger 配置 responseMessageList import springfox com public documentation

pom依赖

<!-- swagger2 配置 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
        </dependency>

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

swagger配置类

package com.javasm.config;

import com.javasm.common.http.EnumStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//访问地址:http://localhost:8088/swagger-ui.html#

//使用com.github.xiaoymin依赖后
// http://localhost:8088/doc.html     原路径
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
    //api接口包扫描路径 
    public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.javasm";
    public static final String VERSION = "1.0.0";
    @Bean
    public Docket createRestApi() {

        //添加全局响应状态码
        List<ResponseMessage> responseMessageList = new ArrayList<>();
        Arrays.stream(EnumStatus.values()).forEach(enumStatus -> {
            responseMessageList.add(
                    new ResponseMessageBuilder().code(enumStatus.getStatus()).message(enumStatus.getMessage()).responseModel(
                            new ModelRef(enumStatus.getMessage())).build()
            );
        });
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .globalResponseMessage(RequestMethod.GET,responseMessageList)
                .globalResponseMessage(RequestMethod.DELETE,responseMessageList)
                .globalResponseMessage(RequestMethod.POST,responseMessageList)
                .globalResponseMessage(RequestMethod.PUT,responseMessageList)
                .select()
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
                .paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档,忽略哪些请求
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring boot  Api ") //设置文档的标题
                .description("Spring boot  Api 接口文档") // 设置文档的描述
                .version(VERSION) // 设置文档的版本信息-> 1.0.0 Version information
                .termsOfServiceUrl("http://www.xxx.com") //自己设置, 设置文档的License信息->1.3 License information
                .build();
    }

}

注解使用场景

@Api(value = "用户中心评价模块", tags = {"用户中心评价模块相关接口"})
@RestController
@RequestMapping("mycomments")
public class MyCommentsController extends BaseController {

}

    @ApiOperation(value = "查询商品评论", notes = "查询商品评论", httpMethod = "GET")
    @GetMapping("/comments")
    public AxiosResult comments(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @RequestParam String itemId,
            @ApiParam(name = "level", value = "评价等级", required = false)
            @RequestParam Integer level,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @RequestParam String userId,
            @RequestBody ShopcartBO shopcartBO
            @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {

             }

 

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
  TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back

标签:swagger,配置,responseMessageList,import,springfox,com,public,documentation
From: https://www.cnblogs.com/sibuxiang-zxb/p/17243252.html

相关文章

  • git一个本地仓库,配置两个远程仓库
    git一个本地仓库,配置两个远程仓库,gitpush的时候会推送到两个远程仓库方法:找到并打开git的配置.git/config。把url改为如下 ......
  • daemon.json 常用配置项
    {"registry-mirrors":["https://fv50tv30.mirror.aliyuncs.com","https://reg-mirror.qiniu.com"],"dns":["114.114.114.114","8.8.8.8"],"dat......
  • SpringMVC配置文件位置
    [size=large][color=red]在工程内:[/color][/size]<context:property-placeholderlocation="classpath:/site.properties"/>[size=large][color=red......
  • Spring MVC视图解析器:配置多个视图解析器的优先级
    [color=red]问题[/color]在SpringMVC应用程序中,我们经常需要应用一些视图解析器策略来解析视图名称。例如,联合使用三个视图解析器:InternalResourceViewResolver、Resourc......
  • npm的常见问题及配置
    运行npminstall遇到问题:Nomatchingversionfoundforts-transformer-properties-rename@~0.14.0运行 npmsearchts-transformer-properties-rename也报错404应......
  • Centos7 下的SVN安装与配置
    ```Centos7下的SVN安装与配置1.关闭防火墙   临时关闭防火墙   systemctlstopfirewalld       永久防火墙开机自关闭   systemctldisablefirewa......
  • knife4J 配置(生成接口文档)
    1、概述knife4j是springfox-swagger的增强UI实现,为Java来发者在使用swagger的时候,能拥有一份简洁、强大的接口文档体验。2、使用1、导入依赖(pom.xml)<depende......
  • 配置docker容器veth-pair---实现桥接模式
    前言:已知docker网络三种基础模式bridge、host、none,·bridge:桥接模式,创建容器时默认的网络模式;docker安装时,在宿主机内创建一个虚拟网桥docker0,并自动给docker......
  • 李沐动手学深度学习环境配置(Win)
    环境配置(Win)一、安装Minicondahttps://docs.conda.io/en/latest/miniconda.html更改镜像源condanotepad.condarcpypipipconfigsetgloba......
  • RN6752V1 HD转MIPI CSI配置文件使用手册
    ​此文件描述如何使用RN6752V1配置表文件(rn6752V1_configuration.h)与配置定义(rn6752V1.h)来开发RN6752V1驱动.RN6752V1是经由I2CBus来配置内部寄存器,开发......