Java Jackson反序列化-空对象引用上的JsonMappingException
我有一个Note类型的object newNote。我正在通过向newNote添加值来合成对象(使用Jackson库),这没关系,但当我尝试这样做时:Java Jackson反序列化-空对象引用上的JsonMappingException,java,android,json,http,jackson,Java,Android,Json,Http,Jackson,我有一个Note类型的object newNote。我正在通过向newNote添加值来合成对象(使用Jackson库),这没关系,但当我尝试这样做时: String bodyRequest = mapper.writeValueAsString(newNote); 我有一个空对象引用的JsonMappingException 我正在尝试撰写一个POST请求的正文,以便在此处插入: URL url = new URL("http://myUrl"); HttpURLCo
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
...
}
使用者
mapper.setSerializationInclusion(Include.NON_NULL);
或
Jackson还允许在ObjectMapper上全局配置此行为:提供stackstrace
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);