使用Jackson(或Spring)解码Base64 [英] Decode Base64 with Jackson (or Spring)

查看:234
本文介绍了使用Jackson(或Spring)解码Base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试处理二进制数据,所以我对此很陌生。
我正在写一个用于上传内容的REST服务,我将收到一个Base64编码的字符串。

This is the first time that I try to handle binary data so I'm quite new to this. I'm writing a REST service for uploading stuff, and I'm going to receive a Base64 encoded String.

我发现这个 (标准Java),我也发现了一个内部的Spring类(坏主意)。

I've found this (standard Java), and I've also found an internal Spring class (bad idea).

是否有一个Jackson注释来自动解码Base64中的属性?
我应该在我的对象中使用String还是byte []?

Is there a Jackson annotation to automatically decode a property from Base64? Should I use String or byte[] in my Object?

我也使用Spring MVC 3,所以可以从执行此操作的Spring框架。

I'm also using Spring MVC 3, so it will be ok to have a class from the Spring framework to do this.

[请,没有Apache Commons。我想找到一个没有添加更多东西的解决方案]

[please, no Apache Commons. I would like to find a solution without adding more stuff]

推荐答案

使用 byte [] 用于属性,Base64编码/解码只是工作。没有别的办法。

Use byte[] for property, and Base64 encoding/decoding "just works". Nothing additional to do.

此外,杰克逊可以通过以下方式进行显式转换:

Additionally, Jackson can do explicit conversion by something like:

ObjectMapper mapper = new ObjectMapper();
byte[] encoded = mapper.convertValue("Some text", byte[].class);
String decoded = mapper.convertValue(encoded, String.class);

如果你想使用Jackson进行独立的Base64编码/解码。

if you want to use Jackson for stand-alone Base64 encoding/decoding.

这篇关于使用Jackson(或Spring)解码Base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆