我的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 { 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) { // }
JSON中的响应如下:
[ { "status": false,"preferredFlag": true },{ "status": true,"preferredFlag": false },"preferredFlag": false } ]
尽管Spring-Data JPA从存储库返回整个对象,为什么其他两个字段,即userId和linkedEmail没有显示出来?
解决方法
默认情况下,Spring Data Rest不再将@Id Properties编组为JSON.
这可以使用曝光ID实现 – 请检查Spring Rest
类似的帖子在这里详细解释 – id not marshalled to JSON
我建议使用中间响应类而不是将实体转换为JSON.