com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(Object o)不会将字段写入JSON字符串

时间:2015-06-05 21:27:30

标签: java json

我需要将Student的对象转换为JSON字符串。 StudentId的对象是Student的属性。当我使用com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(Student s)将Student写入字符串时,我看到所有其他属性都转换为除StudentId之外的JSON字符串。我缺少什么StudentId课程?当我调试时,我确实在调用writeValueAsString(student)之前看到studentId字段,但不知何故没有写入JSON字符串。

public class StudentId extends AbstractLongId{
    public StudentId(final Long id) {
       super(id);
    }

    public StudentId(final String id) {
        super(id);
    }
}

public class Student {
    private DateTime created;
    private StudentId studentId;
    private String name

}

public abstract class AbstractLongId extends AbstractId<Long> {
    public AbstractLongId(final Long value) {
        super(value);
    }

    public AbstractLongId(final String value) {
        super(value);
    }
        @Override
        protected Long fromString(final String value) {
            try {
            return new Long(value);
        }
        catch (NumberFormatException e) {
            throw new IllegalArgumentException("Note valid input");
        }
    }
}

public abstract class AbstractId<T> extends Wrapper<T> implements Id<T> {
    public AbstractId(final T value) {
        super(value);
    }
    public AbstractId(final String value) {
        super(value);
    }
}

public abstract class Wrapper<T> {

    private final T value;

    public Wrapper(final T value) {
        this.value = value;
    }
    public Wrapper(final String value) {
        T typedValue = fromString(value);
        this.value = typedValue;
    }

    protected abstract T fromString(final String value);

    public T getValue() {
        return value;
    }

    @Override
    public String toString() {
        return value.toString();
    }
    public boolean equalsString(final String other) {
        if (other == null) {
            return false;
        }
        return value.toString().equals(other);
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean equals(final Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof Wrapper)) {
            return false;
        }

        Wrapper<T> otherId = (Wrapper<T>) other;

        return value.equals(otherId.getValue());
    }

    @Override
    public int hashCode() {
        return value.hashCode();
    }
}

0 个答案:

没有答案