0

I converted UUID to string (String id) and put the conversion inside a method.

I also declared other String variables such as FirstName etc and put in on an ArrayList: img2

Code

The code does work. But I'm confused why the string email was showing second on the list.

public class StudentController {
    @Autowired
    StudentService studentService = new StudentService();

    @GetMapping
    public List<Student> displayStudent(){
        return studentService.getStudent();
    }
}

public class StudentService {
    Student student = new Student();
    private List<Student> studentList = Arrays.asList(
        new Student(student.genID(),"Elvis" , "Presley" ,"Elvis@gmail.com")
    );
    
    public List<Student> getStudent(){
        return studentList;
    }
}

public class Student {
    UUID uuid = UUID.randomUUID();
    private String id;
    private String FirstName;
    private String LastName;
    private String email;

    public Student() {}

    //Method Converting UUID into string
    public String genID(){
        id = uuid.toString();
        return id;
    }

    public Student(String id) {
        this.id = id;
    }
    
    public Student(String id, String firstName, String lastName, String email) {
        this.id = id;
        FirstName = firstName;
        LastName = lastName;
        this.email = email;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirstName() {
        return FirstName;
    }

    public void setFirstName(String firstName) {
        FirstName = firstName;
    }

    public String getLastName() {
        return LastName;
    }

    public void setLastName(String lastName) {
        LastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

Expected

I expected data to be in this order

ID , FirstName , LastName , email

Actual Output JSON

img1

hc_dev
  • 8,389
  • 1
  • 26
  • 38
Zee
  • 11
  • 2
  • The order you expect is simply the order of the constructor parameters. What would matter here is the order of the fields in the serialization (which may be the order of your fields in the class, or they may be manually specified elsewheres, not terribly familiar with Spring). – Rogue Jul 08 '22 at 17:35
  • 1
    The title is not summarizing your issue: the list is in order (at Java object and at serialized JSON output) but the properties within each element are not ordered as you expected. – hc_dev Jul 08 '22 at 18:15

2 Answers2

1

JSON is an unordered collection, as specified on https://www.json.org/json-en.html , so you don't have to worry about it. It might depend on library though.

0

Specify the serialized order of properties

The order of properties during serialization can be defined in Jackson. Either at class-level specifically using annotation @JsonPropertyOrder. Or globally for your ObjectMapper using a feature:

objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)

Example

In your case you can achieve expected order using the annotation on your class:

@JsonPropertyOrder({'id', 'firstName', 'lastName', 'email'})
public class Student {
  // body of your class
}

Or separately with an index on your fields:

public class Student {
  @JsonProperty(index=10) 
  private String id;

  // not ordered specifically
  private String firstName;
  private String lastName;

  @JsonProperty(index=20) 
  private String email;

  // remainder of your class
}

See also

hc_dev
  • 8,389
  • 1
  • 26
  • 38