使用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串就可以了。

相关推荐
Lucky GGBond29 分钟前
Vue + Spring Boot 实现 Excel 导出实例
vue.js·spring boot·excel
seven97_top36 分钟前
Springboot 常见面试题汇总
java·spring boot·后端
瑶山4 小时前
社区版Idea怎么创建Spring Boot项目?Selected Java version 17 is not supported. 问题解决
java·spring boot·intellij-idea·创建项目
陈小桔4 小时前
SpringBoot之配置文件
spring boot
智能化咨询4 小时前
基于Spring Boot + Vue 3的乡村振兴综合服务平台性能优化与扩展实践
vue.js·spring boot·性能优化
Hoking5 小时前
LangChain4j集成SpringBoot接入百炼大模型(Qwen)
java·人工智能·spring boot·llm
编啊编程啊程8 小时前
【004】生菜阅读平台
java·spring boot·spring cloud·dubbo·nio
椎4958 小时前
web后端开发——原理
spring boot·spring·mybatis
岁岁岁平安8 小时前
Java+SpringBoot+Dubbo+Nacos快速入门
java·spring boot·nacos·rpc·dubbo
小咕聊编程10 小时前
【含文档+PPT+源码】基于springboot的旅游路线推荐系统的设计与实现
spring boot·后端·旅游