Spring 引导部分替换自动配置

Spring Boot partially replacing autoconfiguration

我一直在探索 Spring Boot (1.2.4) 来创建一个简单的 RESTful 存储库,使用 H2 & Jackson。

和其他人一样,我注意到,默认情况下,@Id 字段不在返回的 Json 中(如本问题所述:While using Spring Data Rest after migrating an app to Spring Boot, I have observed that entity properties with @Id are no longer marshalled to JSON)。

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

但是我无法让它全部正常工作。

基本实体class:

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

简单的启动应用程序:

@ComponentScan
@EnableAutoConfiguration
public class Application {    
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }    
}

还有我的配置class:

@Configuration
public class RepositoryRestMvcConfig extends SpringBootRepositoryRestMvcConfiguration {     
    @Override
    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(Thing.class);
    }        
}

我可以从 运行 Spring 使用 --debug 启动(如 http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html 中所述)我的配置已找到,但它似乎没有任何效果.

Jackson 在 class 上处理 getters/setters 而不是成员变量本身。因此,要确保 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; }

}