首页 > Java > 正文

在 Postman 中使用图像文件发送 JSON

WBOY
发布: 2024-02-09 16:24:08
转载
937人浏览过

php小编西瓜将为您介绍如何在postman中使用图像文件发送json。postman是一款功能强大的api开发工具,可以帮助开发人员进行接口测试和调试。通常情况下,我们使用postman发送json数据,但是如果需要在json中包含图像文件,该怎么办呢?本文将详细介绍如何在postman中通过一些简单的步骤实现这一功能,让您更加灵活地使用postman进行接口测试。无论您是初学者还是有经验的开发人员,都能从本文中获得实用的技巧和知识。让我们一起来了解吧!

问题内容

我想在 postman 中发送带有图像的 json 文件,但收到以下错误:

"status": 415,
"error": "unsupported media type",
登录后复制

专家控制器

package com.shayanr.homeservicespring.controller;

import com.shayanr.homeservicespring.entity.users.expert;
import com.shayanr.homeservicespring.service.expertservice;
import lombok.requiredargsconstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.multipartfile;

import java.io.file;
import java.io.ioexception;

@restcontroller
@requestmapping("/expert")
@requiredargsconstructor
public class expertcontroller {
    private expertservice expertservice;

    @postmapping("/register")
    public void register(@requestbody expert expert,
            @requestparam("image") multipartfile image) throws ioexception {
        expertservice.signup(expert, image);
    }
}
登录后复制

专家服务

@override
@transactional
public expert signup(expert expert, multipartfile image) throws ioexception {
    if (expert == null) {
        throw new nullpointerexception("null expert");
    }
    if (!validate.namevalidation(expert.getfirstname()) ||
            !validate.namevalidation(expert.getlastname()) ||
            !validate.emailvalidation(expert.getemail()) ||
            !validate.passwordvalidation(expert.getpassword())) {
        throw new persistenceexception("wrong validation");
    }
    string extension = filenameutils.getextension(image.getname());
    if (extension.equalsignorecase("video") || extension.equalsignorecase("mp4")) {
        throw new persistenceexception("invalid image format. only image files are allowed.");
    }
    expert.setconfirmation(confirmation.new);

    byte[] imageinbytes = image.getbytes();
    expert.setimage(imageinbytes);
    expertrepository.save(expert);
    return expert;

}
登录后复制

在邮递员中,我的第一行是 json 和这个 json 文件

{
  "firstName": "Sasa",
  "lastName": "Weel",
  "email": "[email protected]",
  "password": "Sasasd1@",
  "signUpDate": "2024-01-31",
  "signUpTime": "10:00:00"
}
登录后复制

在第二行中我设置了问题所在的图像

解决方法

您的 @requestparam("image") multipartfile image 参数看起来正确。

因此,您接下来应该做的就是确保告诉 spring 您希望允许多部分文件。

Find JSON Path Online
Find JSON Path Online

Easily find JSON paths within JSON objects using our intuitive Json Path Finder

Find JSON Path Online 30
查看详情 Find JSON Path Online

这是一个示例 application.properties 配置:

spring.servlet.multipart.enabled=true
登录后复制

控制上传大小的其他配置设置:

# Adjust these file size restrictions and location as nessessary
spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.location=/temp
登录后复制

我怀疑 @requestbody expert expert 参数应该与此有关。

以上就是在 Postman 中使用图像文件发送 JSON的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号