第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Jackson 序列化/反序列化異常而不使用堆棧跟蹤

使用 Jackson 序列化/反序列化異常而不使用堆棧跟蹤

HUH函數(shù) 2023-12-13 16:53:53
我正在嘗試創(chuàng)建一個具有java.lang.Exception存儲為字段的類。另外,我嘗試使用@JsonIgnoreProperties注釋從序列化/反序列化中排除堆棧跟蹤。import com.fasterxml.jackson.annotation.JsonIgnoreProperties;import com.fasterxml.jackson.databind.ObjectMapper;import java.io.IOException;class ExWrapper {    @JsonIgnoreProperties({"stackTrace"})    public Exception ex;    @Override    public String toString() {        return "ExWrapper{" +                "ex=" + ex +                '}';    }}public class Example {    public static void main(String[] args) throws IOException {        ObjectMapper mapper = new ObjectMapper();        ExWrapper exw = new ExWrapper();        exw.ex = new Exception("Oops");        String str = mapper.writeValueAsString(exw);        System.out.println(str);        ExWrapper exW = mapper.readValue(str, ExWrapper.class);        System.out.println(exW);    }}好吧,類中只有g(shù)etMessage和沒有,所以這似乎有點合理,除非您嘗試刪除注釋。它的工作原理就像一個魅力:它可以正確地序列化和反序列化回來,并且缺少 setter for突然就不再是問題了。添加到忽略的字段也可以使其工作(但沒有異常消息)。setMessageThrowable@JsonIgnorePropertiesmessage"message"我嘗試使用調(diào)試器隨機(jī)步入 Jackson 代碼,發(fā)現(xiàn)當(dāng)@JsonIgnoreProperties缺少時,最終ThrowableDeserializer會調(diào)用方法,而當(dāng)注釋存在時不會調(diào)用它們。ThrowableDeserializer似乎有一些特定于異常消息的技巧。我的猜測是,ThrowableDeserializer當(dāng)堆棧跟蹤丟失并且 Jackson 回退到默認(rèn)的 java bean 序列化器時,這是不可行的。問題是這里到底發(fā)生了什么以及如何解決它。
查看完整描述

1 回答

?
阿波羅的戰(zhàn)車

TA貢獻(xiàn)1862條經(jīng)驗 獲得超6個贊

版本2

ThrowableDeserializer類擴(kuò)展,BeanDeserializer因此這兩個共享一些如何創(chuàng)建和反序列化POJO. Exception不是常規(guī)的POJO,應(yīng)該以不同的方式處理。由于它沒有提供許多設(shè)置器,我們需要使用構(gòu)造函數(shù)來使用消息和其他我們可以跳過的字段來創(chuàng)建它。要注冊構(gòu)造函數(shù),我們可以使用MixIn功能:


import com.fasterxml.jackson.annotation.JsonCreator;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import com.fasterxml.jackson.annotation.JsonProperty;

import com.fasterxml.jackson.databind.ObjectMapper;


import java.io.File;


public class JsonPathApp {


    public static void main(String[] args) throws Exception {

        File jsonFile = new File("./resource/test.json").getAbsoluteFile();


        ObjectMapper mapper = new ObjectMapper();

        mapper.addMixIn(Exception.class, ExceptionMixIn.class);

        mapper.addMixIn(Throwable.class, ThrowableMixIn.class);


        ExWrapper exW = mapper.readValue(jsonFile, ExWrapper.class);

        exW.ex.printStackTrace();

    }

}


@JsonIgnoreProperties("stackTrace")

abstract class ExceptionMixIn extends Exception {


    @JsonCreator

    public ExceptionMixIn(@JsonProperty("message") String message) {

        super(message);

    }

}


@JsonIgnoreProperties("stackTrace")

abstract class ThrowableMixIn extends Throwable {


    @JsonCreator

    public ThrowableMixIn(@JsonProperty("message") String message) {

        super(message);

    }

}


class ExWrapper {


    public Exception ex;


    @Override

    public String toString() {

        return "ExWrapper{" +

                "ex=" + ex +

                '}';

    }

}

JSON上面的帶有原因的異常代碼打?。?/p>


java.lang.Exception: Opps

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)

    at com.fasterxml.jackson.databind.introspect.AnnotatedConstructor.call(AnnotatedConstructor.java:124)

    at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:283)

    at com.fasterxml.jackson.databind.deser.ValueInstantiator.createFromObjectWith(ValueInstantiator.java:229)

    at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:195)

    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:422)

    at com.fasterxml.jackson.databind.deser.std.ThrowableDeserializer.deserializeFromObject(ThrowableDeserializer.java:65)

    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)

    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)

    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)

    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)

    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)

    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2902)

Caused by: java.lang.Throwable: Root oops

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)

    at com.fasterxml.jackson.databind.introspect.AnnotatedConstructor.call(AnnotatedConstructor.java:124)

    at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:283)

    at com.fasterxml.jackson.databind.deser.ValueInstantiator.createFromObjectWith(ValueInstantiator.java:229)

    at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:195)

    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:422)

    at com.fasterxml.jackson.databind.deser.std.ThrowableDeserializer.deserializeFromObject(ThrowableDeserializer.java:65)

    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)

    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:530)

    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:528)

    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:449)

    ... 8 more

如您所見,異常具有默認(rèn)值stackTrace,并且會stackTrace跳過JSON有效負(fù)載。


版本1

我沒有深入挖掘它,但啟用allowSetters解決了這個問題:


@JsonIgnoreProperties(value = {"stackTrace"}, allowSetters = true)

public Exception ex;

進(jìn)行此更改的代碼將打?。?/p>


{"ex":{"cause":null,"message":"Oops","localizedMessage":"Oops","suppressed":[]}}

ExWrapper{ex=java.lang.Exception: Oops}


查看完整回答
反對 回復(fù) 2023-12-13
  • 1 回答
  • 0 關(guān)注
  • 211 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號