English 中文(简体)
绕过最后领域
原标题:Jackson deserialization circumventing final fields

这里的法典

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.ToString;

public class Main {
    public static void main(String[] args) throws Exception {
        Fields f1 = new Fields(1);
        System.out.println(f1);

        ObjectMapper mapper = new ObjectMapper();
        String str = mapper.writeValueAsString(f1);
        System.out.println(str);

        Fields f2 = mapper.readValue(str, Fields.class);
        System.out.println(f2);
    }

    @Data
    @ToString
    public static class Fields {
        private final long value1;
        private final long value2;

        public Fields(@JsonProperty("blah") long value) {
            this.value1 = value++;
            this.value2 = value++;
            System.out.println(this);
        }
    }
}

产出

Main.Fields(value1=1, value2=2)
Main.Fields(value1=1, value2=2)
{"value1":1,"value2":2}
Main.Fields(value1=0, value2=1)
Main.Fields(value1=1, value2=2)

我的问题是:

  • Why did jackson modify private final fields that do not have setters after finish constructing it? If this is intended, how do I turn it off?
  • If jackson is able to set fields directly, why is it required that I annotate the constructor with @JsonProperty? (Removing @JsonProperty from Fields results in error; and I didn t even need to annotate with correct properties)

谢谢。

最佳回答

为什么我需要用@JsonProperty来通知施工者?

不是。 需要的是无障碍建筑。 你们要么可以有一个无参数的建筑。

public Fields() {
    this.value1 = 0;
    this.value2 = 0;
    System.out.println("cons: " + this);
}

(由于这些田地为final) ,或者你可以有一个建筑商,由Jackson试图根据已申报的<代码>@JsonProperty解决。) 注:JsonProperty#required>:code>false.

Why did jackson modify private final fields that do not have setters after finish constructing it? If this is intended, how do I turn it off?

因为它能够做到。 因此,它使你能够使用可变型的降水。 我不知道你通过什么方法可以消除这一特征。

问题回答

为什么在完成建造后,劫持者会改变没有固定的私人最后领域? 如果打算这样做,我如何去掉?

页: 1 (t is true byault) when configuating theirmapper.

Example:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Temp {

    private static final String RAW = "{"value1": "aabbcc","value2":"zzzzz"}";

    public static void main(String[] args) throws Exception {
        System.out.println(new ObjectMapper().readValue(RAW, TestClass.class));

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // you will receive UnrecognizedPropertyException without this line
        mapper.configure(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS, false);

        System.out.println(mapper.readValue(RAW, TestClass.class));
    }

    public static class TestClass {

        private final String value1;
        private final String value2;

        @JsonCreator
        public static TestClass createTestClass(
                @JsonProperty("value1") String value1,
                @JsonProperty("blah") String value2) {

            return new TestClass(value1, value2);
        }

        private TestClass(String value1, String value2) {
            this.value1 = value1;
            this.value2 = value2;
        }

        public String getValue1() {
            return value1;
        }

        public String getValue2() {
            return value2;
        }

        @Override
        public String toString() {
            return "TestClass{" + "value1=" + value1 + ", value2=" + value2 +  } ;
        }
    }
}

产出:

TestClass{value1=aabbcc, value2=zzzzz}
TestClass{value1=aabbcc, value2=null}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签