使用Postman对@RequestPart和HttpServletRequest组合传参方式

使用Postman对@RequestPart和HttpServletRequest组合传参方式

方法代码如下:

java 复制代码
    /**
     * 发布
     */
    @ApiOperation("发布")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "req", value = "json格式", dataType = "Map", dataTypeClass = Map.class),
            @ApiImplicitParam(name = "file", value = "文件", dataType = "File", dataTypeClass = File.class)
    })
    @PostMapping("/publish")
    public AjaxResult publish(@Validated @RequestPart("req") BizDemandInfo entity, HttpServletRequest request) throws IOException {
        List<MultipartFile> fileList = ((MultipartHttpServletRequest) request).getFiles("file");
        // 限制上传文件大小,单位是字节
        final long MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
        // 限制上传文件的数量
        final int MAX_FILES = 5;
        List<BizDemandAttachment> attachmentList = new ArrayList<>();
        if (!fileList.isEmpty()) {
            //上传文件数不能大于5
            if (fileList.size() > MAX_FILES) {
                log.error("上传文件数不能大于5");
                return AjaxResult.error("上传文件数不能大于5");
            }
            for (MultipartFile file : fileList) {
                //每个文件不能大于10M
                if (file.getSize() > MAX_FILE_SIZE) {
                    log.error("每个文件不能大于10M");
                    return AjaxResult.error("每个文件不能大于10M");
                }
                String url = FileUploadUtils.uploadMinio(Constants.MINIO_BUCKET_NAME_DEMAND, file);
                String fileName = file.getOriginalFilename();
                BizDemandAttachment attachment = new BizDemandAttachment();
                attachment.setAttachmentName(fileName);
                attachment.setAttachmentUrl(url);
                attachmentList.add(attachment);
            }
        }
        entity.setBizDemandAttachmentList(attachmentList);
        String createBy = getUsername();
        entity.setCreateBy(createBy);
        String id = service.insert(entity);
        return StringUtils.isNoneBlank(id) ? AjaxResult.success(id) : AjaxResult.error();
    }

想使用postman对entity和request参数传参,应该怎么传呢?

传参方法:

直接上图片

因为参数entity是@RequestPart("req") BizDemandInfo entity,所以名称得填req。最重要的是Content-Type要填application/json

里面的内容直接填json串就可以了。

相关推荐
小马爱打代码3 分钟前
Spring Boot 3.4 :@Fallback 注解 - 让微服务容错更简单
spring boot·后端·微服务
旷世奇才李先生25 分钟前
奇哥面试记:SpringBoot整合RabbitMQ与高级特性,一不小心吊打面试官
spring boot·面试·java-rabbitmq
经典19921 小时前
spring boot 详解以及原理
java·spring boot·后端
白仑色2 小时前
Spring Boot 多环境配置详解
java·spring boot·后端·微服务架构·配置管理
超级小忍2 小时前
在 Spring Boot 中优化长轮询(Long Polling)连接频繁建立销毁问题
java·spring boot·后端
Z_W_H_3 小时前
【springboot】IDEA手动创建SpringBoot简单工程(无插件)
java·spring boot·intellij-idea
荔枝hu4 小时前
springboot生成pdf方案之dot/html/图片转pdf三种方式
spring boot·pdf·html
好奇的菜鸟4 小时前
在 Postman 中高效生成随机环境变量的完整指南
测试工具·lua·postman
Apipost的同学们4 小时前
AI时代的接口自动化优化实践:如何突破Postman的局限性
后端·ai·架构·postman·自定义函数·apipost·api+ai
Z_W_H_7 小时前
【SpringBoot】 整合MyBatis+Postgresql
java·spring boot·后端