我的对象是注释类型的新注释。我通过向newNote添加值来组合对象(使用Jackson库),但是当我尝试这样做时:
String bodyRequest = mapper.writeValueAsString(newNote);
我在null对象引用上有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);
我的Note课程是:
@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
答案 0 :(得分:0)
用户
mapper.setSerializationInclusion(Include.NON_NULL);
OR
mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
Jackson还允许在ObjectMapper上全局配置此行为: