Search code examples
javanulljson-deserializationrest-assureddto

Get null fields after deserialization java JSON


I assume a problen in class structure. But I do not see where. Here is a code. I'm gonna use only some values from JSON. If you need some other code part just let me know. Maily I think I get objest (user) wuth null values here:

User userDTOGetOne = userRequester.getUser
  (
    token,
    new HashMap<String, Object>() {{ put("Uuid", userResponseDTO.getUserUuid()); }}
   )
   .getBody()
   .as(User.class);

I suppose here is a problem .as(User.class) with class structure. I have compared them not once and I can not find a problem. Thank you in advance.


Solution

  • Finally, I found out your problems.

    1.@JsonRootName("User") in User class doesn't work, I have to create a separate UserDTo class.

    import com.fasterxml.jackson.annotation.JsonProperty;
    import lombok.Data;
    
    @Data
    public class UserDTO {
        @JsonProperty("User")
        private User user;
    }
    

    2.In class User

    private HashMap<String, HashMap<String, CustomFieldDTO> > UserCustomFields;
    

    should be

    private Map<String, CustomFieldDTO> UserCustomFields;
    

    3.In class CustomFieldDTO

    private HashMap<String, CustomFieldVariableDTO> WebAdmin;
    private HashMap<String, CustomFieldVariableDTO> Client;
    

    should be

    private CustomFieldVariableDTO WebAdmin;
    private CustomFieldVariableDTO Client;
    

    I make a simple test

    UserDTO user = res.as(UserDTO.class);
    System.out.println(user);
    
    UserDTO(user=User(Uuid=b5ee1186-7355-4810-a422-c427c1ae420c, 
    Username=golden_snich_testt, [email protected], FirstName=Bob, 
    LastName=Sigal, Birthdate=1995-01-07T00:00:00Z, Phone=+12698741258, 
    Notes=null, Locked=false, PostPayLimit=10.0, HighSchoolUuid=null, 
    IsVerifiedHighSchoolUser=false, UserCustomFields={9cebfaaa-ca66-4bbd-baa3- 
    557b0645c293=CustomFieldDTO(FieldUuid=null, FieldType=String, 
    FieldName=null, WebAdmin=CustomFieldVariableDTO(Status=0, 
    AllowChangeStatus=false), Client=CustomFieldVariableDTO(Status=0, 
    AllowChangeStatus=false), IsDefault=false, 
    SerializedValue=Nickyfieldname213241), bb854ba1-8ef7-4cfb-9fe7- 
    8c6d9a0551b2=CustomFieldDTO(FieldUuid=null, FieldType=String, 
    FieldName=null, WebAdmin=CustomFieldVariableDTO(Status=0, 
    AllowChangeStatus=false), Client=CustomFieldVariableDTO(Status=0, 
    AllowChangeStatus=false), IsDefault=false, 
    SerializedValue=Nickyfieldname2)}))