ResourceBundle

时间:2016-03-02 19:04:03

标签: java json jackson serializable resourcebundle

所以我的问题如下: 对于我需要编写的应用程序,我必须实现将某些DTO存储到磁盘以便以后重用的能力(采用JSON格式)。只是为您提供一个广泛的参考框架:DTO包含过程/数据模型以及它们的图形表示。

要获取所需的JSON文件,我目前使用Jackson。这对于最大的部分来说效果很好,但是,在一个需要保存的对象中我使用ResourceBundle(用于本地化不同语言的程序)。这正是问题出现的地方,因为杰克逊似乎无法序列化ResourceBundle对象(知道这两者都是尝试它,而且我到目前为止所做的研究基本上也告诉了我同样的事情。)

所以我想问你是否可能知道如何使它工作,或者你是否找到了一些奇特的解决方法。

为了进一步说明,我将附加一些不是来自相关项目的示例代码,因为我是为其他人做的,我不确定他是否会欣赏他的代码的发布。

public class SomeClass {
  private String name;
  private ResourceBundle bundle;

  public SomeClass(String name, ResourceBundle bundle) {
    this.name = name;
    this.bundle = bundle;
  }

  public String getName() {
    return this.name;
  }

  public ResourceBundle getBundle() {
    return this.bundle;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setBundle(ResourceBundle bundle) {
    this.bundle = bundle;
  }

  /*
    Here one could imagine some additional functionality making use of the 
    given ResourceBundle (something that has to be printed depending on the
    used language etc.). 
  */
}
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;

public class Saver {

  public static void main(String[] args) {
    Saver saver = new Saver();
    saver.run();
  }

  public void run() {
    ObjectMapper om = new ObjectMapper();
    ResourceBundle rb = ResourceBundle.getBundle("test");
    SomeClass sc = new SomeClass("SomeClass", rb);
    try {
      om.writeValue(new File("test.json"), sc);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

生成的堆栈跟踪如下所示:

com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class sun.util.ResourceBundleEnumeration and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: SomeClass["bundle"]->java.util.PropertyResourceBundle["keys"])
  at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:230)
  at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:68)
  at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:32)
  at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:672)
  at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
  at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
  at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:672)
  at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
  at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
  at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)
  at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3613)
  at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:2929)
  at Saver.run(Saver.java:22)
  at Saver.main(Saver.java:14)

1 个答案:

答案 0 :(得分:0)

如果JSON Java类包含可能会破坏应用程序的递归循环,则应该检查它。如果是这种情况,请将@JsonIgnore添加到相关属性以打破循环。