杰克逊混合不起作用

时间:2016-09-21 08:12:43

标签: json jackson mixins fasterxml

这是第三方课(让我们假设为例)

public class User {
private Integer userId;
private String password;

public User(Integer userId, String password) {
    this.userId = userId;
    this.password = password;
}

public Integer getUserId() {
    return userId;
}

public String getPassword() {
    return password;
}

@Override
public String toString() {
    return "User [userId=" + userId + ", password=" + password + "]";
}
}

这是相应的混音类

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public abstract class UserMixin {

@JsonProperty("userId")
abstract String getUserId();

@JsonProperty("password")
abstract String getPassword();

@JsonCreator
public UserMixin(
        @JsonProperty("userId") Integer userId
        , @JsonProperty("password") String password) {
    System.out.println("mixed-in constructor.");
}
}

这是相应的Driver类

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonMixIn {
public static void main(String[] args) throws IOException {

    User user = new User(1,  "password");

    ObjectMapper mapper = new ObjectMapper();
    final String json = mapper.writeValueAsString(user);
    System.out.println(json);

    ObjectMapper mapper2 = new ObjectMapper();
    mapper2.addMixIn(User.class, UserMixin.class);

    System.out.println(mapper2.writeValueAsString(user));

    final User deserializedUser = mapper2.readValue(json, User.class);
    System.out.println(deserializedUser);
}
}

如果我运行驱动程序类,每个东西都像魅力一样工作,并按预期获得输出。

{"userId":1,"password":"password"}
{"userId":1,"password":"password"}
User [userId=1, password=password]

但是,如果我删除以下用户类的getter

public Integer getUserId() {
   return userId;
}

public String getPassword() {
   return password;
}

我收到以下异常

线程“main”中的异常com.fasterxml.jackson.databind.JsonMappingException:没有为类com.github.dexecutor.redisson.User找到序列化器,也没有发现创建BeanSerializer的属性(为了避免异常,禁用SerializationFeature) .FAIL_ON_EMPTY_BEANS))     at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:269)     在com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:68)     at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:32)     在com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)     at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3631)     at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2998)     在com.github.dexecutor.redisson.JacksonMixIn.main(JacksonMixIn.java:13)

1 个答案:

答案 0 :(得分:0)

所有应该做的就是以下事情,事情应该像魅力

一样
mapper.setVisibility(mapper.getSerializationConfig()
        .getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
            .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));

有关详细信息,请参阅this