Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Not sending all fields in JSON response

MY POJO:

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;

import lombok.Data;


@Entity
@Table(name="user_linked_email")
@IdClass(UserLinkedEmailKey.class)
@Data
public class UserLinkedEmail implements Serializable {


    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    @Id
    private Integer userId;
    @Id
    private String linkedEmail;


    /**
     * The Following are appearing in JSON response
     */
    private boolean status;

    private boolean preferredFlag;

}

UserLinkedEmailKey class:

public class UserLinkedEmailKey implements Serializable {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    private Integer userId;
    private String linkedEmail;
}

And My Controller Snippet:

public org.springframework.http.ResponseEntity<?> getLinkedEmails(@PathVariable(value = "userId") Integer zepoUserId) {


        try {
            List<UserLinkedEmail> linkedEmails = userService.getLinkedEmails(zepoUserId);


            //linkedEmails till this point has all 4 fields

            return new ResponseEntity<List<UserLinkedEmail>>(linkedEmails, HttpStatus.OK);

        } catch (Exception e) {
            //
        }

Response in JSON is as follows:

[
  {
    "status": false,
    "preferredFlag": true
  },
  {
    "status": true,
    "preferredFlag": false
  },
  {
    "status": true,
    "preferredFlag": false
  }
]

Why are the other two fields i.e userId and linkedEmail not showing up in response although Spring-Data JPA is returning the entire object from the repository?

like image 479
Koustav Ray Avatar asked Jan 20 '17 06:01

Koustav Ray


1 Answers

By default, Spring Data Rest no longer marshalls @Id Properties to JSON.

This can be achieved using expose Ids - Please check Spring Rest

Similar post here with detailed explanation- id not marshalled to JSON

I would suggest to have Intermediate Response Class instead of converting Entity to JSON.

like image 154
Srikanth A Avatar answered Oct 14 '22 22:10

Srikanth A