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

相关推荐
学Java的bb1 天前
后端Web实战-Spring原理
java·spring boot·spring
码畜也有梦想1 天前
springboot响应式编程笔记
java·spring boot·笔记
Codebill1 天前
API 响应体加密场景下的调试实践:Postman 的局限与 Apipost 的优化
postman·apipost·api调试
王同学 学出来1 天前
跟做springboot尚品甄选项目(二)
java·spring boot·后端
计算机毕业设计指导1 天前
基于Spring Boot + Vue 3的社区养老系统设计与实现
vue.js·spring boot·后端
从零开始学习人工智能1 天前
SpringBoot + Apache Tika:一站式解决文件数据提取难题
spring boot·后端·apache
上官浩仁1 天前
springboot maven 多环境配置入门与实战
java·spring boot·maven
麦兜*1 天前
Docker 部署 MongoDB:单节点与副本集的最佳实践
java·spring boot·mongodb·spring cloud·docker·容器·maven
小程序设计1 天前
【springboot+vue】高校迎新平台管理系统(源码+文档+调试+基础修改+答疑)
vue.js·spring boot·后端
波波烤鸭1 天前
Spring Boot 原理与性能优化实战
spring boot·后端·性能优化