How to implement multiple file uploads based on Swagger 3.x in Spring boot 3.x

news2024/10/5 17:27:07

How to implement multiple file uploads based on Swagger 3.x in Spring boot 3.x

  • Project
    • pom.xml
    • OpenAPIConfig
    • FileUploadController
    • application.yaml

Project

pom.xml

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.qwfys.sample</groupId>
    <artifactId>upload-sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>upload-sample</name>
    <description>upload-sample</description>

    <properties>
        <java.version>17</java.version>
        <swagger.starter.version>1.9.1.RELEASE</swagger.starter.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.0.3</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

OpenAPIConfig

package com.qwfys.sample.upload.base.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class OpenAPIConfig {

    @Value("${app.openapi.dev-url}")
    private String devUrl;

    @Value("${app.openapi.prod-url}")
    private String prodUrl;

    @Bean
    public OpenAPI myOpenAPI() {
        Server devServer = new Server();
        devServer.setUrl(devUrl);
        devServer.setDescription("Server URL in Development environment");

        Server prodServer = new Server();
        prodServer.setUrl(prodUrl);
        prodServer.setDescription("Server URL in Production environment");

        Contact contact = new Contact();
        contact.setEmail("qwfys200@qq.com");
        contact.setName("qwfys200");
        contact.setUrl("https://www.qwfys.com");

        License mitLicense = new License().name("MIT License").url("https://choosealicense.com/licenses/mit/");

        Info info = new Info()
                .title("Tutorial Management API")
                .version("1.0")
                .contact(contact)
                .description("This API exposes endpoints to manage tutorials.").termsOfService("https://www.bezkoder.com/terms")
                .license(mitLicense);

        return new OpenAPI().info(info).servers(List.of(devServer, prodServer));
    }
}

FileUploadController

package com.qwfys.sample.upload.controller;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@Tag(name = "Tutorial", description = "Tutorial management APIs")
@RestController
public class FileUploadController {

    private static final Logger logger = LoggerFactory.getLogger(FileUploadController.class);

    @RequestMapping(value = "/uploadMultiFiles", consumes = {"multipart/form-data"}, method = RequestMethod.POST)
    public String uploads(@RequestPart(required = true) MultipartFile[] files) {
        logger.info("File Upload:{}", files);
        return "sucessfull";
    }

    @RequestMapping(value = "/uploadMultiFile", consumes = {"multipart/form-data"}, method = RequestMethod.POST)
    public String uploads(@RequestPart(required = true) MultipartFile file) {
        logger.info("File Upload:{}", file);
        return "sucessfull";
    }
}

application.yaml

spring:
  servlet:
    multipart:
      max-file-size: 5MB
      max-request-size: 5MB
      enabled: true
      location: ${java.io.tmpdir}

app:
  openapi:
    dev-url: http://localhost:8080
    prod-url: https://api.qwfys.com/sample

在这里插入图片描述
在这里插入图片描述

参考文献

  • Manage upload/download of files using REST and Spring Boot
  • Spring Boot File upload example with Multipart File
  • Spring Boot + Swagger 3 example (with OpenAPI 3)
  • Swagger 3 annotations in Spring Boot
  • Setting up Swagger 3 With Spring Boot
  • Springfox Reference Documentation
  • How to run Swagger 3 on Spring Boot 3
  • Swagger UI 3 Setup with Spring Boot Application
  • Uploading Multiple Files with Springfox and Swagger-UI
  • Spring Boot Example for Uploading Swagger Files

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1467681.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【科普知识】什么是电机的开环和闭环

电机是现代工业和生活中不可或缺的一部分&#xff0c;无论是电动工具、电动汽车还是工业机器人&#xff0c;都离不开电机的驱动。电机的控制系统根据有无反馈信号可以分为开环和闭环两种类型&#xff0c;这两种系统各有其特点和应用场景。 01.开环控制系统 开环控制系统是电机控…

AP引擎助力加速生产SQL运行

Rapid存储引擎简介 从GreatSQL 8.0.32-25版本开始&#xff0c;新增Rapid存储引擎&#xff0c;该引擎使得GreatSQL能满足联机分析&#xff08;OLAP&#xff09;查询请求。 Rapid引擎采用插件&#xff08;Plugin&#xff09;方式嵌入GreatSQL中&#xff0c;可以在线动态安装或卸…

(九)springmvc+mybatis+dubbo+zookeeper分布式架构 整合 - maven构建ant-framework核心代码Base封装

今天重点讲解的是ant-framework核心代码Base封装过程。 因为涉及到springmvc、mybatis的集成&#xff0c;为了使项目编码更简洁易用&#xff0c;这边将基础的BASE进行封装&#xff0c;其中包括&#xff1a;BaseBean、BaseDao、BaseService、CRUD的基础封装、分页组件的封装、m…

OSCP靶场--Nickel

OSCP靶场–Nickel 考点(1.POST方法请求信息 2.ftp&#xff0c;ssh密码复用 3.pdf文件密码爆破) 1.nmap扫描 ┌──(root㉿kali)-[~/Desktop] └─# nmap 192.168.237.99 -sV -sC -p- --min-rate 5000 Starting Nmap 7.92 ( https://nmap.org ) at 2024-02-22 04:06 EST Nm…

22款奔驰C260L升级小柏林音响 无损音质效果

奔驰新款C级号称奔驰轿车的小“S”&#xff0c;在配置方面上肯定也不能低的&#xff0c;提了一台低配的车型&#xff0c;通过后期升级加装件配置提升更高档次&#xff0c;打造独一无二的奔驰C级&#xff0c;此次来安排一套小柏林之声音响&#xff0c;效果怎么样&#xff0c;我们…

maven3旧版本的下载地址(含新版本)

因为现有的3.8版本与IDEA不兼容&#xff0c;我需要下载3.6版本&#xff0c;但是官网的位置非常隐蔽&#xff0c;找了很多资料才看到。故记录一下。 第一步 进入网址&#xff0c;选择需要的版本 Index of /dist/maven/maven-3 第二步 选择binaries 第三步 选择zip文件下载就可…

Nacos配置中心实战

目录 配置中心 什么是Nacos配置中心&#xff1f; SpringCloud整合Nacos配置中心 nacos server配置中心中准备配置数据 微服务接入配置中心 Config相关配置 RefreshScope实现动态感知 配置中心 在微服务架构中&#xff0c;当系统从一个单体应用&#xff0c;被拆分成分布式…

Qt应用-音乐播放器实例

本文讲解Qt音乐播放器应用实例。 实现主要功能 声音播放、暂停,拖动控制、声音大小调节; 播放列表控制; 歌词显示; 界面设计 pro文件中添加 # 播放媒体 QT += multimedia 头文件 #ifndef FRMMUSICPLAYER_H #define FRMMUSICPLAYER_H#include <QWidget> #include…

陆毅小女油画惊艳,11岁已超越王诗龄。

♥ 为方便您进行讨论和分享&#xff0c;同时也为能带给您不一样的参与感。请您在阅读本文之前&#xff0c;点击一下“关注”&#xff0c;非常感谢您的支持&#xff01; 文 |猴哥聊娱乐 编 辑|徐 婷 校 对|侯欢庭 陆毅假期宅家享天伦之乐&#xff0c;晒出二女儿小叶子的画作&…

wordpress免费主题模板

免费大图wordpress主题 首页是一张大图的免费wordpress主题模板。简洁实用&#xff0c;易上手。 https://www.jianzhanpress.com/?p5857 wordpress免费模板 动态效果的wordpress免费模板&#xff0c;banner是动态图片效果&#xff0c;视觉效果不错。 https://www.jianzhan…

Beyond Compare4破解方法

方式一 第一种办法&#xff08;也是最有效的&#xff09; 删除C:\Users\用户名\AppData\Roaming\Scooter Software\Beyond Compare 4下的所有文件&#xff0c;重启Beyond Compare 4即可&#xff08;注意&#xff1a;用户名下的AppData文件夹有可能会被隐藏起来) 方式二 删…

第2讲-Memory

存储系统概述 存储单元电路

万字干货-京东零售数据资产能力升级与实践

开篇 京东自营和商家自运营模式&#xff0c;以及伴随的多种运营视角、多种组合计算、多种销售属性等数据维度&#xff0c;相较于行业同等量级&#xff0c;数据处理的难度与复杂度都显著增加。如何从海量的数据模型与数据指标中提升检索数据的效率&#xff0c;降低数据存算的成…

论文阅读——SimpleClick

SimpleClick: Interactive Image Segmentation with Simple Vision Transformers 模型直接在VIT上增加交互是分割 用VIT MAE方法训练的预训练权重 用交互式分割方法微调&#xff0c;微调流程&#xff1a; 1、在当前分割自动模拟点击&#xff0c;没有人为提供的点击 受到RITM启发…

BERT学习笔记

论文&#xff1a;《BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding》&#xff0c;2019 代码&#xff1a;[tensorflow]&#xff0c;[pytorch] 来源&#xff1a;李沐精度BERT 0、摘要 与之前模型的区别&#xff1a; GPT考虑的是一个单向…

FairyGUI × Cocos Creator 3.7.3 引入报错解决

Cocos Creator 3.7.3引入fgui库 package.json添加这个依赖 "devDependencies": {"fairygui-cc": "latest"}执行npm i 报错解决 使用import引入fairygui-cc&#xff0c;就会有报错和警告&#xff0c;简单处理一下。 鼠标随便点一下也会出警告…

300分钟吃透分布式缓存-13讲:如何完整学习MC协议及优化client访问?

协议分析 异常错误响应 接下来&#xff0c;我们来完整学习 Mc 协议。在学习 Mc 协议之前&#xff0c;首先来看看 Mc 处理协议指令&#xff0c;如果发现异常&#xff0c;如何进行异常错误响应的。Mc 在处理所有 client 端指令时&#xff0c;如果遇到错误&#xff0c;就会返回 …

Ubuntu20.04 查看系统版本号

目录 uname -auname -vlsb_release -acat /etc/issuecat /proc/version uname -a 查看系统发行版本号和操作系统版本 uname -v 查看版本号 lsb_release -a 查看发行版本信息 cat /etc/issue 查看系统版本 cat /proc/version 查看内核的版本号

Linux笔记--文件与目录

ls /--查看根目录 一、介绍 1.目录结构 // 5.3 FHS (Filesystem Hierarchy Standard ) 文件层次结构标准 (标准规范每个特定的目录应该放什么 bin:全称 binary&#xff0c;含义是二进制。该目录中存储的都是一些二进制文件&#xff0c;文件都是可以被运行的。(一些最经常使…