搜索
首页Java在 Postman 中使用图像文件发送 JSON

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 您希望允许多部分文件。

这是一个示例 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中文网其他相关文章!

声明
本文转载于:stackoverflow。如有侵权,请联系admin@php.cn删除

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中