我的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类:
public class UserLinkedEmailKey implements Serializable { /** * serialVersionUID */ private static final long serialVersionUID = 1L; private Integer userId; private String linkedEmail; }
和我的控制器片段:
public org.springframework.http.ResponseEntity> getLinkedEmails(@PathVariable(value = "userId") Integer zepoUserId) { try { ListlinkedEmails = userService.getLinkedEmails(zepoUserId); //linkedEmails till this point has all 4 fields return new ResponseEntity >(linkedEmails, HttpStatus.OK); } catch (Exception e) { // }
JSON中的响应如下:
[ { "status": false, "preferredFlag": true }, { "status": true, "preferredFlag": false }, { "status": true, "preferredFlag": false } ]
尽管Spring-Data JPA从存储库返回整个对象,为什么其他两个字段即userId和linkedEmail没有显示在响应中?
默认情况下,Spring Data Rest不再将@Id Properties编组为JSON.
这可以通过暴露Ids来实现 - 请检查Spring Rest
类似的帖子在这里有详细的解释 - 没有编组到JSON
我建议使用中间响应类而不是将实体转换为JSON.