The output will look like the structure of the User object :
{"id":1,"index":"{\"_id\":2}"}
Not sur what you want to do. Your output format apparently not correct. What you want to display looks like a list. You would have to wrap the user object in a list to get the desired result.
Also, the "id" does not appear in your output format. Do you want to have the id value in the index directly ? You need to rethink your object or create another object to fill the output.
one track could be to change the name of json on id field by adding this :
@JsonProperty("_id")
private int id;
To have your User format try this :
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
User user = new User();
user.setId(1);
mapper.writeValue(new File("user.json"), new Index(user));
}
@Data
public static class User {
@JsonProperty("_id")
private int id;
public User() {
}
}
@Data
public static class Index {
@JsonProperty("index")
private User user;
public Index(User user) {
this.user = user;
}
}
For me, it's definitivly a list you want Output will like this for one object :
{"index":{"_id":1}}