1

I have a POJO class A and I want the fields to be in the order I defined, but '1800number' appears last in the response since it starts with a number. How do I avoid that?

 @JsonInclude(JsonInclude.Include.NON_NULL)
 public class A {
    private String name;
    private String age;
    private String dept;
    @JsonProperty("1800number")
    private String _1800number;
    private String email;
 } 

This is how I get the response

{
  "name": "Mark",
  "age": "52",
  "dept": "Civil",
  "email": "mark@gmail",
  "1800number": "12345"
}

I use below libraries In my gradle

 compile('com.fasterxml.jackson.core:jackson-annotations:2.8.3')
 compile('com.fasterxml.jackson.core:jackson-databind:2.8.3')
user3919727
  • 283
  • 2
  • 7
  • 25
  • 1
    what kind of library are you using? how are you creating the json? also, dont forget to add the tag for that library. else the experts wont find ur question. right now, the question is just unclear as long as that stuff is missing. – Zabuzard Jun 03 '19 at 20:52
  • 1
    Zabuza, updated the post above with the library – user3919727 Jun 03 '19 at 21:01

1 Answers1

2

Use @JsonPropertyOrder annotation :

@JsonPropertyOrder({ "name", "age", "dept", "1800number", "email" })
public class A {
    private String name;
    private String age;
    private String dept;
    @JsonProperty("1800number")
    private String _1800number;
    private String email;
 } 
Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63