当前位置: 首页 > 知识库问答 >
问题:

Jackson ObjectMapper如何将byte[]传输到String,以及如何在没有对象类的情况下翻译它?

米嘉禧
2023-03-14

我想开发restful服务,它将向客户端返回JSON字符串。现在我的对象中有byte[]属性。

我使用ObjectMapper将此对象翻译为json并响应客户端。但是我发现字节[]是错误的,如果我使用String.getBytes()来翻译接收到的字符串。下面是例子。

Pojo类

public class Pojo {
    private byte[] pic;
    private String id;
    //getter, setter,...etc
}
InputStream inputStream = FileUtils.openInputStream(new File("config/images.jpg"));
byte[] pic = IOUtils.toByteArray(inputStream);
Pojo pojo = new Pojo();
pojo.setId("1");
pojo.setPic(pic);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(pojo);

--情形1:使用readvalue创建对象=

Pojo tranPojo = mapper.readValue(json, Pojo.class);

byte[] tranPicPojo = tranPojo.getPic();
InputStream isPojo = new ByteArrayInputStream(tranPicPojo);
File tranFilePojo = new File("config/images2.png");
FileUtils.copyInputStreamToFile(isPojo, tranFilePojo);

--情形2:使用readvalue映射并获取字符串=

Map<String, String> map = mapper.readValue(json, Map.class);

byte[] tranString = map.get("pic").getBytes();
InputStream isString = new ByteArrayInputStream(tranString);
File tranFileString = new File("config/images3.png");
FileUtils.copyInputStreamToFile(isString, tranFileString);

如果我必须使用情况2来翻译JSON字符串,我该怎么做?因为客户端无法获得Pojo。类,因此客户端只能自己翻译JSON字符串。

谢谢!

共有2个答案

秦俊友
2023-03-14

ObjectMapper处理这个,通过单元测试表示:

@Test
public void byteArrayToAndFromJson() throws IOException {
    final byte[] bytes = { 1, 2, 3, 4, 5 };

    final ObjectMapper objectMapper = new ObjectMapper();

    final byte[] jsoned = objectMapper.readValue(objectMapper.writeValueAsString(bytes), byte[].class);

    assertArrayEquals(bytes, jsoned);
}

这是杰克逊2. x顺便说一句...

以下是如何使用Jersey将文件发送到浏览器:

@GET
@Path("/documentFile")
@Produces("image/jpg")
public Response getFile() {
    final byte[] yourByteArray = ...;
    final String yourFileName = ...;
    return Response.ok(yourByteArray)
                   .header("Content-Disposition", "inline; filename=\"" + yourFilename + "\";")
                   .build();

另一个例子,使用Spring MVC:

@RequestMapping(method = RequestMethod.GET)
public void getFile(final HttpServletResponse response) {
    final InputStream yourByteArrayAsStream = ...;
    final String yourFileName = ...;

    response.setContentType("image/jpg");
    try {
        output = response.getOutputStream();
        IOUtils.copy(yourByteArrayAsStream, output);
    } finally {
        IOUtils.closeQuietly(yourByteArrayAsStream);
        IOUtils.closeQuietly(output);
    }
}
松博耘
2023-03-14

Jackson正在将字节[]序列化为Base64字符串,如序列化程序文档中所述。

默认的base64变量是不带换行符的MIME(所有内容都在一行中)。

您可以使用ObjectMapper上的setbase64变量来更改变量。

 类似资料:
  • TL;DR:将@webservlet(“/find-customers”)放在servlet(通过Tomcat 7部署)的开头并不是将servlet映射到host:port/webproject/find-customers中,即使servlet位于src文件夹中。

  • 更新:我知道我应该使用JS和/或Ajax来实现它,我只是想知道是否有一种方法也可以在PHP中实现它:)

  • 问题内容: Netbeans仔细地将Logger.getLogger(this.getClass()。getName())。log(Level。[…]语句撒到catch块中。现在,我想将它们全部指向一个文件(并指向控制台)。 每个日志教程(只有我这样)都告诉我如何获取特定的日志记录器以输出到文件中,但是我认为有比修复每个自动生成的日志记录语句更好的方法了?为某种类型的根记录程序设置处理程序? 问题

  • 问题内容: 我正在尝试设置spring xml配置,而不必创建进一步的。但是,即使我将数据库属性包括在 spring.xml: 我在这里想念什么? 问题答案: 在entityManagerFactory bean定义中指定“ packagesToScan”和“ persistenceUnitName”属性。 请注意,这适用于Spring版本> 3.1

  • 问题内容: 我正在学习使用Selenium(v2.20)来领先一些 即将使用它创建浏览器测试的程序员。我想在 陷阱到达之前发现它们,而我却跌入了一个陷阱。 当我创建ChromeDriver时,它始终会弹出“ Google Chrome EULA”并 显示两个按钮:“接受并运行”和“取消”。因为我希望这是一个 自动化测试,所以让用户单击按钮是不可能的。 我查看了Chromium CommandSwi

  • 我想查看项目中的所有类,当我找到一个从“City”派生的类时,我想创建一个该类型的新对象并将其添加到列表中。这允许我添加功能,而无需更新列表。最重要的是,我想在不使用任何库的情况下完成它。我已经找到了类似的主题,但他们使用了org。反思。我希望避免以下情况: