gpt4 book ai didi

java - Jackson 反序列化 - 空对象引用上的 JsonMappingException

转载 作者:可可西里 更新时间:2023-11-01 16:41:11 25 4
gpt4 key购买 nike

我有一个 Note 类型的对象 newNote。我正在通过向 newNote 添加值来组合对象(使用 Jackson 库),这没问题,但是当我尝试这样做时:

String bodyRequest = mapper.writeValueAsString(newNote);

我在空对象引用上有 JsonMappingException。

我正在尝试编写 POST 请求的主体以插入此处:

URL url = new URL("http://myUrl");

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", mAuth);
connection.setDoOutput(true);

//here I try to set up the body
String bodyRequest = mapper.writeValueAsString(newNote);



DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());

//here I have the other problem
dStream.writeBytes(bodyRequest);
dStream.flush();
dStream.close();

但是当我尝试时也有错误:

dStream.writeBytes(bodyRequest);

我这样配置我的 ObjectMapper:

mapper = new ObjectMapper();
mapper.registerModule(new ThreeTenModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.enable(SerializationFeature.USE_EQUALITY_FOR_OBJECT_ID);
mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
mapper.configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, false);
mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
mapper.configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, false);
mapper.configure(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY, false);
mapper.configure(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS, false);

我的笔记类是:

@Entity
@JsonIdentityInfo(generator = IdentityGenerator.class)
@JsonIgnoreProperties(ignoreUnknown = true)

public class Note {

@Id
private Long id;
private User author;
private ZonedDateTime created;
private String content;
private Status status;
private User assignee;
private Activity activity;
private List<File> attachments = new ArrayList<>();
private Boolean restricted;
private Status NextStatus;
private User NextAssignee;

...
//setters and getters
//hashcode and equals methods
...
}

我该怎么办?

编辑

这是我的堆栈跟踪:

05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err:       com.fasterxml.jackson.databind.JsonMappingException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:141)
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3613)
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2980)
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at it.prova.www.b.TicketsNewNotes$PostAsyncCaller.doInBackground(TicketsNewNotes.java:399)
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at it.prova.www.b.TicketsNewNotes$PostAsyncCaller.doInBackground(TicketsNewNotes.java:342)
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at java.lang.Thread.run(Thread.java:818)
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at it.prova.www.b.best.base.dto.identity.IdentityGenerator.generateId(IdentityGenerator.java:73)
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at it.prova.www.b.best.base.dto.identity.IdentityGenerator.generateId(IdentityGenerator.java:12)
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at com.fasterxml.jackson.databind.ser.impl.WritableObjectId.generateId(WritableObjectId.java:43)
05-04 11:32:48.259 4754-5030/it.prova.www.b W/System.err: at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase._serializeWithObjectId(BeanSerializerBase.java:588)
05-04 11:32:48.260 4754-5030/it.prova.www.b W/System.err: at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:148)
05-04 11:32:48.260 4754-5030/it.prova.www.b W/System.err: at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)
05-04 11:32:48.260 4754-5030/it.prova.www.b W/System.err: ... 10 more

最佳答案

用户

mapper.setSerializationInclusion(Include.NON_NULL);

 mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);

Jackson 还允许在 ObjectMapper 上全局配置此行为:

关于java - Jackson 反序列化 - 空对象引用上的 JsonMappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37021094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com