将字节数组解码为字符串格式错误。 [Java]

Decoding byte array into String format error. [Java]

我的应用程序将推文加载到 Oracle NoSQL 数据库,然后检索它们,并使用 Jackson 库解析 JSON 以获得我需要的值。

我的代码中有 2 个问题,但我很确定一个问题来自另一个问题。第一个问题来自将我从数据库中获取的字节数组 (byte []) 解码为字符串。

这是 JSON 字符串的推文是如何编码并保存到数据库中的:

Value myValue = Value.createValue(Base64.getEncoder().encode(msg.value().getBytes()));

这会将来自字节数组的推文作为 Value object 保存到数据库中。

现在,我需要检索这个值对象,并获取它存储的字节数组。然后将它解码成一个字符串,最后用 Jackson 解析它。解码是这样完成的:

String data = new String(value.toByteArray(),StandardCharsets.UTF_8);

而这实际上是 returns 一个字符串。但是那个字符串肯定不是 JSON 格式的推文,而是这个怪物:

eyJjcmVhdGVkX2F0IjoiRnJpIE1heSAwMyAxMDozOToyNCArMDAwMCAyMDE5IiwiaWQiOjExMjQyNjIxNzQ1Njg5MjcyMzIsImlkX3N0ciI6IjExMjQyNjIxNzQ1Njg5MjcyMzIiLCJ0ZXh0IjoiUG9tcGVvOiBPbWFyXHUyMDE5cyBWZW5lenVlbGEgY29tbWVudHMgXHUyMDE4ZGlzZ3VzdGluZ1x1MjAxOSBcdTIwMTMgRm94XHUwMGEwTmV3cyBodHRwczpcL1wvdC5jb1wvNDY2UTNZS1pLeiBodHRwczpcL1wvdC5jb1wvNGRKSFpWeVJiMCIsImRpc3BsYXlfdGV4dF9yYW5nZSI6WzAsODFdLCJzb3VyY2UiOiJcdTAwM2NhIGhyZWY9XCJodHRwOlwvXC9wdWJsaWNpemUud3AuY29tXC9cIiByZWw9XCJub2ZvbGxvd1wiXHUwMDNlV29yZFByZXNzLmNvbVx1MDAzY1wvYVx1MDAzZSIsInRydW5jYXRlZCI6ZmFsc2UsImluX3JlcGx5X3RvX3N0YXR1c19pZCI6bnVsbCwiaW5fcmVwbHlfdG9fc3RhdHVzX2lkX3N0ciI6bnVsbCwiaW5fcmVwbHlfdG9fdXNlcl9pZCI6bnVsbCwiaW5....

等等。我在一个副项目中做了一些测试,看看这个转换是否有效并且一切顺利。这是用于测试的 class:

import java.io.*;
import java.util.*;
import java.math.*;
import bs.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Caca {

static String tocode = "{\"created_at\":\"Tue Apr 23 09:10:22 +0000 2019\",\"id\":1120615888883994624,\"id_str\":\"1120615888883994624\",\"text\":\"RT @chuckwoolery: Largest OIL and GAS find in Historyin West Texas. Dwarfs Saudi, Iran, and Iraq. Did you hear about this In the MSM? NO.\u2026\",\"source\":\"\u003ca href=\\"http:\/\/twitter.com\/download\/iphone\\" rel=\\"nofollow\\"\u003eTwitter for iPhone\u003c\/a\u003e\",\"truncated\":false,\"in_reply_to_status_id\":null,\"in_reply_to_status_id_str\":null,\"in_reply_to_user_id\":null,\"in_reply_to_user_id_str\":null,\"in_reply_to_screen_name\":null,\"user\":{\"id\":3545646375,\"id_str\":\"3545646375\",\"name\":\"Tom Willoughby\",\"screen_name\":\"willogus\",\"location\":\"USA\",\"url\":null,\"description\":\"MAJ. USA (Ret) \u201cDUSTOFF\u201d\/\/Retired-Teacher\/Administrator\/ Independent Voter\",\"translator_type\":\"none\",\"protected\":false,\"verified\":false,\"followers_count\":414,\"friends_count\":115,\"listed_count\":1,\"favourites_count\":9322,\"statuses_count\":22038,\"created_at\":\"Fri Sep 04 14:48:46 +0000 2015\",\"utc_offset\":null,\"time_zone\":null,\"geo_enabled\":false,\"lang\":\"en\",\"contributors_enabled\":false,\"is_translator\":false,\"profile_background_color\":\"000000\",\"profile_background_image_url\":\"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png\",\"profile_background_image_url_https\":\"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png\",\"profile_background_tile\":false,\"profile_link_color\":\"4A913C\",\"profile_sidebar_border_color\":\"000000\",\"profile_sidebar_fill_color\":\"000000\",\"profile_text_color\":\"000000\",\"profile_use_background_image\":false,\"profile_image_url\":\"http:\/\/pbs.twimg.com\/profile_images\/901525176495599621\/t2G36W62_normal.jpg\",\"profile_image_url_https\":\"https:\/\/pbs.twimg.com\/profile_images\/901525176495599621\/t2G36W62_normal.jpg\",\"default_profile\":false,\"default_profile_image\":false,\"following\":null,\"follow_request_sent\":null,\"notifications\":null},\"geo\":null,\"coordinates\":null,\"place\":null,\"contributors\":null,\"retweeted_status\":{\"created_at\":\"Mon Apr 22 13:01:07 +0000 2019\",\"id\":1120311572730191873,\"id_str\":\"1120311572730191873\",\"text\":\"Largest OIL and GAS find in History in West Texas. Dwarfs Saudi, Iran, and Iraq. Did you hear about this In the MSM\u2026 https:\/\/t.co\/Eptox0vNEx\",\"source\":\"\u003ca href=\\"http:\/\/twitter.com\\" rel=\\"nofollow\\"\u003eTwitter Web Client\u003c\/a\u003e\",\"truncated\":true,\"in_reply_to_status_id\":null,\"in_reply_to_status_id_str\":null,\"in_reply_to_user_id\":null,\"in_reply_to_user_id_str\":null,\"in_reply_to_screen_name\":null,\"user\":{\"id\":462104542,\"id_str\":\"462104542\",\"name\":\"Chuck Woolery\",\"screen_name\":\"chuckwoolery\",\"location\":\"Texas\",\"url\":\"http:\/\/bluntforcetruth.com\/\",\"description\":\"Hollywood Conservative, Love Connection Host, Back in Two and Two, Fisherman, Navy Vet, Political Activist. Host of Blunt Force Truth w\/ @markyoungtruth\",\"translator_type\":\"none\",\"protected\":false,\"verified\":true,\"followers_count\":543465,\"friends_count\":2856,\"listed_count\":3854,\"favourites_count\":9982,\"statuses_count\":75643,\"created_at\":\"Thu Jan 12 15:30:41 +0000 2012\",\"utc_offset\":null,\"time_zone\":null,\"geo_enabled\":true,\"lang\":\"en\",\"contributors_enabled\":false,\"is_translator\":false,\"profile_background_color\":\"200E82\",\"profile_background_image_url\":\"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png\",\"profile_background_image_url_https\":\"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png\",\"profile_background_tile\":false,\"profile_link_color\":\"0084B4\",\"profile_sidebar_border_color\":\"C0DEED\",\"profile_sidebar_fill_color\":\"DDEEF6\",\"profile_text_color\":\"333333\",\"profile_use_background_image\":true,\"profile_image_url\":\"http:\/\/pbs.twimg.com\/profile_images\/1764172727\/Chuck_Headshot_normal.jpg\",\"profile_image_url_https\":\"https:\/\/pbs.twimg.com\/profile_images\/1764172727\/Chuck_Headshot_normal.jpg\",\"profile_banner_url\":\"https:\/\/pbs.twimg.com\/profile_banners\/462104542\/1499952548\",\"default_profile\":false,\"default_profile_image\":false,\"following\":null,\"follow_request_sent\":null,\"notifications\":null},\"geo\":null,\"coordinates\":null,\"place\":null,\"contributors\":null,\"is_quote_status\":false,\"extended_tweet\":{\"full_text\":\"Largest OIL and GAS find in History in West Texas. Dwarfs Saudi, Iran, and Iraq. Did you hear about this In the MSM? NO. Will you? No. If you do it will be the end of the World because it's fossil fuel. Good news for the average American.\",\"display_text_range\":[0,238],\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[],\"symbols\":[]}},\"quote_count\":110,\"reply_count\":181,\"retweet_count\":4868,\"favorite_count\":11615,\"entities\":{\"hashtags\":[],\"urls\":[{\"url\":\"https:\/\/t.co\/Eptox0vNEx\",\"expanded_url\":\"https:\/\/twitter.com\/i\/web\/status\/1120311572730191873\",\"display_url\":\"twitter.com\/i\/web\/status\/1\u2026\",\"indices\":[117,140]}],\"user_mentions\":[],\"symbols\":[]},\"favorited\":false,\"retweeted\":false,\"filter_level\":\"low\",\"lang\":\"en\"},\"is_quote_status\":false,\"quote_count\":0,\"reply_count\":0,\"retweet_count\":0,\"favorite_count\":0,\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[{\"screen_name\":\"chuckwoolery\",\"name\":\"Chuck Woolery\",\"id\":462104542,\"id_str\":\"462104542\",\"indices\":[3,16]}],\"symbols\":[]},\"favorited\":false,\"retweeted\":false,\"filter_level\":\"low\",\"lang\":\"en\",\"timestamp_ms\":\"1556010622603\"}";

    static Tweet tw = null;

    public static void main(String[] args) {

        byte[] encoded = Base64.getEncoder().encode(tocode.getBytes());
        byte[] encoded2 = tocode.getBytes(StandardCharsets.UTF_8);
        System.out.println("byte[]encoded is: " + encoded);

        //String decoded = Base64.getDecoder().decode(encoded2).toString();
        String decoded = new String(encoded2,StandardCharsets.UTF_8);
        System.out.println("Decoded is: " + decoded);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        try {
            tw = objectMapper.readValue(decoded, Tweet.class);
        } catch (IOException ex) {
            Logger.getLogger(Caca.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println(tw.toString());
    }
}

第二部分涉及 Jackson 解析,只要字符串反序列化按预期进行,它就可以正常工作。并且在这个副项目中有效。

关于为什么在主项目上解码不正确有什么想法吗?全部都在 docker 容器上,所以我无法在本地使用实际的 Value 对象进行测试。提前致谢。

虽然您应该能够在这里使用 base64 编码,但您不必这样做。您当前正在从字符串转换为字节,然后通过应用 base64 编码将这些字节转换为另一种形式的字节。假设你可以安全地传输任意字节,那么使用 base64 只是无缘无故地添加了一层转换。只是将文本转换为字节,指定编码更健壮:

Value myValue = Value.createValue(msg.value().getBytes("UTF-8"));