1. 程式人生 > >Protostuff反序列化失敗,導致Tomcat程式shutdown問題

Protostuff反序列化失敗,導致Tomcat程式shutdown問題

Protostuff是一個基於protobuf實現的序列化方法。前段時間,初次接手專案,需要將java bean儲存到redis中,一切編碼完畢,上測試環境,程式很快就會停止,後來檢視tomcat日誌,發現如下錯誤:

後來經過多次定位,發現新寫的java bean,新寫了帶參的建構函式,沒有新增預設的無參建構函式。而一個類如果已經寫建構函式,編譯的時候jvm不會預設新增無參建構函式,這就導致了問題。在Protostuff原始碼中有這麼一段程式碼,需要使用對應java bean的無參建構函式,這樣就導致瞭如上錯誤的出現。

public Schema<T> getSchema() {
            Schema<T> schema = this.schema;
            if(schema == null) {
                synchronized(this) {
                    schema = this.schema;
                    if(this.schema == null) {
                        if(Message.class.isAssignableFrom(this.typeClass)) {
                            try {
                                Message<T> m = (Message)this.typeClass.newInstance();//使用的無參建構函式建立類
this.schema = schema = m.cachedSchema(); } catch (InstantiationException var5) { throw new RuntimeException(var5); } catch (IllegalAccessException var6) { throw new RuntimeException(var6); } } else { this.schema = schema = this.strategy.newSchema(this.typeClass); } } } }