I have some problem with Spring JPA, MongoDB and Jackson when I use @JsonProperty to use a different name for the json/db and java field.
I receive json and save them into collections, for example
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Document(collection="message")
public class Message implements java.io.Serializable{
private static final long serialVersionUID = 1L;
@Id
private String id;
@JsonProperty("raw")
private Raw raw;
@JsonProperty("meta")
private Meta meta;
}
with Raw :
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Document(collection="raw")
public class Raw {
private String dlc;
@JsonProperty("can_id")
private String canId;
@JsonProperty("can_data")
private String canData;
}
I'm using lombok for the get,set and constructor. I had the message into database before change the field name (using for instance canData instead of can_data) and when I retrieved the object from database this renamed fields were null. So I removed the message and stored again and finally work, I made this because into mongodb I see _class:"com.domain.Message".
This is the first problem: every time I change my java class all the stored data give me this problem?
But I have problem even with manually inserted json into Mongodb: the fields where I use @JsonProperty are null when I make the query like this:
@Query("{ 'can_id' : ?0 }")
Severity findByCanId(String canId);
Severity is so:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Document(collection="severity")
public class Severity implements java.io.Serializable{
private static final long serialVersionUID = 1L;
@JsonProperty("can_id")
@Indexed
private String canId;
@JsonProperty("can_name")
private String canName;
private Integer ss;
private Integer ps;
private Integer fs;
private Integer os;
}
The query return me a Severity object with canId and canName null.
Do you know how can I fix this problem? Thanks