2016-09-21 96 views
0

這是一個第三方的類(假設例如緣故)傑克遜。混合不工作

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."); 
} 
} 

下面是相應的驅動程序類

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] 

但是如果我刪除User類

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.fa sterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:269) at 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) 在com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue( ObjectMapper.java:3631) 在com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2998) 在com.github.dexecutor.redisson.JacksonMixIn.main(JacksonMixIn.java:13)

+0

我認爲,這是它是什麼,框架是不是智能足夠(或設計)工作與這種情況下 https://github.com/FasterXML/jackson-databind/issues/1380#issuecomment-248753018 – craftsmannadeem

+0

我已經添加了答案[這裏](https://github.com/FasterXML/jackson-databind/issues/1380) – craftsmannadeem

+0

我已經記錄了這種行爲[這裏](https://reachmnadeem.wordpress.com/2016/09/23/jackson-mixin-to-the-rescue/) – craftsmannadeem

回答

0

全部t帽子應該做的是下面的,事情應該像一個魅力

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

參考this更多細節