Spring 弹簧靴部分取代自动配置
我一直在探索SpringBoot(1.2.4)以使用H2和Jackson创建一个简单的RESTful存储库 与其他人一样,我注意到,默认情况下,@Id字段不在返回的Json中(如本问题所述:) 所以我想调整配置以包含它们 但我很难让这一切正常运作 基本实体类:Spring 弹簧靴部分取代自动配置,spring,spring-boot,spring-data,spring-restcontroller,Spring,Spring Boot,Spring Data,Spring Restcontroller,我一直在探索SpringBoot(1.2.4)以使用H2和Jackson创建一个简单的RESTful存储库 与其他人一样,我注意到,默认情况下,@Id字段不在返回的Json中(如本问题所述:) 所以我想调整配置以包含它们 但我很难让这一切正常运作 基本实体类: @Entity public class Thing { @Id @GeneratedValue(strategy=GenerationType.AUTO) private long id; //I want t
@Entity
public class Thing {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id; //I want this to appear in the RESTful JSON
private String someInformation;
protected Thing(){};
public String getSomeInformation() { return someInformation; }
public void setSomeInformation(String someInformation) { this.someInformation = someInformation; }
}
基本存储库界面:
public interface ThingRepository extends PagingAndSortingRepository<Thing, Long> {
List<Thing> findBySomeInformation(@Param("SomeInformation") String someInformation);
}
和我的配置类:
@Configuration
public class RepositoryRestMvcConfig extends SpringBootRepositoryRestMvcConfiguration {
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(Thing.class);
}
}
通过使用--debug(如中所述)运行Spring Boot,我可以看到我的配置已经找到,但似乎没有任何效果。Jackson处理类上的getter/setter,而不是成员变量本身。因此,要确保id在JSON中,只需为其添加一个getter:
@Entity
public class Thing {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id; //I want this to appear in the RESTful JSON
private String someInformation;
protected Thing(){};
//this is what is needed
public long getId() { return id; }
public String getSomeInformation() { return someInformation; }
public void setSomeInformation(String someInformation) { this.someInformation = someInformation; }
}
为id属性添加getter/setter?如果添加getter和setter,它将起作用
@Entity
public class Thing {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id; //I want this to appear in the RESTful JSON
private String someInformation;
protected Thing(){};
//this is what is needed
public long getId() { return id; }
public String getSomeInformation() { return someInformation; }
public void setSomeInformation(String someInformation) { this.someInformation = someInformation; }
}