Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 弹簧靴部分取代自动配置_Spring_Spring Boot_Spring Data_Spring Restcontroller - Fatal编程技术网

Spring 弹簧靴部分取代自动配置

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

我一直在探索SpringBoot(1.2.4)以使用H2和Jackson创建一个简单的RESTful存储库

与其他人一样,我注意到,默认情况下,@Id字段不在返回的Json中(如本问题所述:)

所以我想调整配置以包含它们

但我很难让这一切正常运作

基本实体类:

@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; }

}