Spring启动@ResponseBody不会序列化实体ID
有一个奇怪的问题,无法弄清楚如何处理它。 有简单的POJO:
@Entity @Table(name = "persons") public class Person { @Id @GeneratedValue private Long id; @Column(name = "first_name") private String firstName; @Column(name = "middle_name") private String middleName; @Column(name = "last_name") private String lastName; @Column(name = "comment") private String comment; @Column(name = "created") private Date created; @Column(name = "updated") private Date updated; @PrePersist protected void onCreate() { created = new Date(); } @PreUpdate protected void onUpdate() { updated = new Date(); } @Valid @OrderBy("id") @OneToMany(mappedBy = "person", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) private List<PhoneNumber> phoneNumbers = new ArrayList<>(); public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getMiddleName() { return middleName; } public void setMiddleName(String middleName) { this.middleName = middleName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public Date getCreated() { return created; } public Date getUpdated() { return updated; } public List<PhoneNumber> getPhoneNumbers() { return phoneNumbers; } public void addPhoneNumber(PhoneNumber number) { number.setPerson(this); phoneNumbers.add(number); } @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); } } @Entity @Table(name = "phone_numbers") public class PhoneNumber { public PhoneNumber() {} public PhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } @Id @GeneratedValue private Long id; @Column(name = "phone_number") private String phoneNumber; @ManyToOne @JoinColumn(name = "person_id") private Person person; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); } }
并rest端点:
@ResponseBody @RequestMapping(method = RequestMethod.GET) public List<Person> listPersons() { return personService.findAll(); }
在json响应中,除了Id之外,我还需要在前端编辑/删除人员。 我怎样才能configuration春季启动序列化ID以及?
这就是现在的反应:
[{ "firstName": "Just", "middleName": "Test", "lastName": "Name", "comment": "Just a comment", "created": 1405774380410, "updated": null, "phoneNumbers": [{ "phoneNumber": "74575754757" }, { "phoneNumber": "575757547" }, { "phoneNumber": "57547547547" }] }]
UPD有双向Hibernate映射,也许它有点问题。
我最近有同样的问题,这是因为这是默认情况下spring-boot-starter-data-rest
工作原理。 看到我的SO问题 – > 在将应用程序迁移到Spring Boot之后使用Spring Data Rest时,我观察到使用@Id的实体属性不再编组为JSON
要定制它的行为,可以扩展RepositoryRestConfigurerAdapter
以公开特定类的ID。
import org.springframework.context.annotation.Configuration; import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; @Configuration public class RepositoryConfig extends RepositoryRestConfigurerAdapter { @Override protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { config.exposeIdsFor(Person.class); } }
来自@ eric-peladan的回答并没有开箱即用,但是非常接近,也许在Spring Boot的早期版本中起作用。 现在这是应该如何configuration,如果我错了纠正我:
import org.springframework.context.annotation.Configuration; import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; @Configuration public class RepositoryConfiguration extends RepositoryRestConfigurerAdapter { @Override public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { config.exposeIdsFor(User.class); config.exposeIdsFor(Comment.class); } }
Spring Boot你必须扩展SpringBootRepositoryRestMvcConfiguration
如果使用RepositoryRestMvcConfiguration ,application.properties中定义的configuration可能不起作用
@Configuration public class MyConfiguration extends SpringBootRepositoryRestMvcConfiguration { @Override protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { config.exposeIdsFor(Project.class); } }
但是对于一个临时的需求你可以使用投影来在序列化中包含id ,如:
@Projection(name = "allparam", types = { Person.class }) public interface ProjectionPerson { Integer getIdPerson(); String getFirstName(); String getLastName();
}
简单的方法:重命名你的variablesprivate Long id;
private Long Id;
为我工作。 你可以在这里阅读更多
嗯,好吧,好像我find了解决scheme。 从pom文件中删除spring-boot-starter-data-rest,并将@JsonManagedReference添加到phoneNumbers和@JsonBackReference中,以提供所需的输出。 Json的回应是不是很漂亮,但现在它有Id。 不知道什么魔术弹簧引导在这种依赖引擎盖下执行,但我不喜欢它:)