Java Spring MVC表blob字段转换为json
我在数据库表中有一个blob字段。它包含特定的信息。我需要将此字段中的数据作为json传递给Rest。Rest使用SpringMVCJava Spring MVC表blob字段转换为json,java,json,spring-mvc,blob,Java,Json,Spring Mvc,Blob,我在数据库表中有一个blob字段。它包含特定的信息。我需要将此字段中的数据作为json传递给Rest。Rest使用SpringMVC @RequestMapping (value = "/blob", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application / json; charset = utf-8") 为了传输blob,我提出了一个到String的转换,String中的客户端将再次获取byte[]
@RequestMapping (value = "/blob", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application / json; charset = utf-8")
为了传输blob,我提出了一个到String的转换,String中的客户端将再次获取byte[]并读取它
这是我的查询和设置转换器
Query <?> Q = session.createNativeQuery (sql);
q.setReadOnly (true);
q.setResultTransformer (BasicTransformerAdapterBlob.INSTANCE);
list = q.getResultList ();
事实证明,有些字符是以� 客户端无法解密它们
如何解决这个问题
还有什么其他机制可以将blob字段转换为json?在这种情况下可以使用Base 64表示。将
BLOB
数据转换为Base64字符串,然后传递给客户端。任何客户都可以解码并轻松使用它
如果您使用的是Java8,则可以将类直接放入包中:
import java.util.Base64;
用于编码到Base64的代码将更改为:
public String encodeBase64(byte [] encodeMe){
byte[] encodedBytes = Base64.getEncoder().encode(encodeMe);
return new String(encodedBytes) ;
}
同样,你的新解码也会随着
public byte[]decodeBase64(String encodedData){
byte[] decodedBytes = Base64.getDecoder().decode(encodedData.getBytes());
return decodedBytes ;
}
交换字符串中字节的最好方法应该是使用Thankyou,这是一种替代方法。但仍然有一个问题。SQLDeveloper将此blob字段正确保存为文本UTF-8。是否可以重复在SQLDeveloper中的操作?您是否尝试过使用类似“选择转换(使用utf8的列)作为表中的解码数据”的查询来转换此数据;
public String encodeBase64(byte [] encodeMe){
byte[] encodedBytes = Base64.getEncoder().encode(encodeMe);
return new String(encodedBytes) ;
}
public byte[]decodeBase64(String encodedData){
byte[] decodedBytes = Base64.getDecoder().decode(encodedData.getBytes());
return decodedBytes ;
}