java - 是否可以将一个 Json 对象中的图像和字符串返回给客户端

标签 java json spring

我正在做一个项目,我试图向客户端返回一个带有图像和其他一些数据的 Json 对象,这可能吗?如果不是,如果我将图像作为字节数组或 base64 返回,前端将能够将其转换回实际图像,还是应该在我这边完成转换?

    {
        "author": "Uponn",
        "title": "Some title",
        "likes": 10000000,
        "file": *image here*,
        "uploadTime": null
    }

最佳答案

答案是:取决于。

I'm doing a project of mine and I'm trying to return an Json object with an image and some other data to the client, is this possible?

您是定义应用程序 API/接口(interface)的人。如果您希望此服务返回包含该数据的 JSON 对象,那么可以:这是可能的。

if I return the image as a byte array or base64 would a frontender be able to convert it

当然。如果您明确指定该服务到底在做什么。所以:当你的后端从磁盘读取某些图像的原始字节,并将这些字节(以某种方式编码,可能使用 base64)放入字符串中时。当然,那么任何客户端都应该能够重新构建原始字节,然后根据用户想要使用这些信息的目的来处理这些信息。

换句话来说:你所要求的一切在技术上都是不可能的。您要理解的关键是:我们无法告诉您您的要求。您必须确定人们为什么以及如何使用您的服务。然后您以支持商定的用例的方式设计它们。

关于java - 是否可以将一个 Json 对象中的图像和字符串返回给客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56295301/

相关文章:

java - 将 Json 数据放入 TextView 和 ImageView 时不稳定

java - 如何从 json 转换为 java 对象?

java - Activity.java中的多个选中项RecyclerView

java - Spring @Autowired @Lazy

Android 中的 javax.management

java - Log4j2:如果已存在,则记录到不同的文件

java - Spring Boot,Spring Security 与数据库链接

java - Spring更改cglib

java - "java.vendor"和 "java.vm.vendor"有什么区别?

Java 泛型类型绑定(bind)到具有特定注释的类