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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With