解決 Jackson反序列化 Unexpected token ... , expected VALUE_STRING: need JSON String that contains type id

首先看到這類提示錯誤,我們應該知道這個肯定是鎖獲取的數據不符合JSON格式

例如在Redis中我們存儲的如下集合

[
            {
                "inviterId": 1747,
                "userName": "x1**[email protected]",
                "currencyId": null,
                "symbol": null,
                "feeAmount": 132973232.86878297748704000000,
                "commissionFee": "132973232.868"
            },
            {
                "inviterId": 447,
                "userName": "1*[email protected]",
                "currencyId": null,
                "symbol": null,
                "feeAmount": 113770242.85038540344352000000,
                "commissionFee": "113770242.85"
            }
        ]

一看便知道不符合Json的格式規範(鍵值),一般情況下你默認使用RedisTemplate<Object, Object>中字符串的的get方法去獲取額時候,因爲不符合格式,反序列化會失敗。哪我們如何解決啦。添加如下的代碼既可以解決問題

  @Bean
	public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
		StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
		stringRedisTemplate.setConnectionFactory( factory );
		return stringRedisTemplate;
	}

我們去解析的時候注入

@Resource(name = "stringRedisTemplate")
    private RedisTemplate<String, String> stringRedisTemplate;

然後使用該對象去獲取值,即可正常解決該問題。

this.stringRedisTemplate.opsForValue().get("COMMON:KEY").toString();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章