我正在使用最新版本的spring-boot,并尝试按照here
所述向我的json实体添加id如果我添加此配置,我的JSON响应将不再起作用:
2015-03-02 11:55:13.949 DEBUG 8288 --- [nio-8080-exec-1] o.s.b.a.e.mvc.EndpointHandlerMapping : Looking up handler method for path /hal/tutorials/1
2015-03-02 11:55:13.951 DEBUG 8288 --- [nio-8080-exec-1] o.s.b.a.e.mvc.EndpointHandlerMapping : Did not find handler method for [/hal/tutorials/1]
2015-03-02 11:55:13.974 DEBUG 8288 --- [nio-8080-exec-1] o.s.b.a.e.mvc.EndpointHandlerMapping : Looking up handler method for path /error
2015-03-02 11:55:13.975 DEBUG 8288 --- [nio-8080-exec-1] o.s.b.a.e.mvc.EndpointHandlerMapping : Did not find handler method for [/error]
这是我的主要配置文件,我尝试将RepositoryConfig
移动到它自己的文件中并使用@Component而不是@Configuration,但没有任何作用。
@SpringBootApplication
@EntityScan(basePackages = { "com.mypp.domain" })
@EnableJpaRepositories(basePackages = { "com.mypp.repository" })
@EnableTransactionManagement
@EnableGlobalMethodSecurity
@EnableJpaAuditing
@EnableConfigurationProperties
public class ShelltorialsApplication {
public static void main(String[] args) {
SpringApplication.run(ShelltorialsApplication.class, args);
}
}
@Configuration
public class RepositoryConfig extends
RepositoryRestMvcConfiguration {
@Override
protected void configureRepositoryRestConfiguration(
RepositoryRestConfiguration config) {
config.exposeIdsFor(Tutorial.class);
}
}
我做错了什么?
答案 0 :(得分:1)
我的工作正常,我需要延长SpringBootRepositoryRestMvcConfiguration
而不是RepositoryRestMvcConfiguration
@Configuration
public class RepositoryConfig extends
SpringBootRepositoryRestMvcConfiguration {
@Override
protected void configureRepositoryRestConfiguration(
RepositoryRestConfiguration config) {
config.exposeIdsFor(Tutorial.class);
}
}
如果您希望所有jpa实体公开其id值,您可以执行以下操作
添加此依赖项:
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections-maven</artifactId>
<version>0.9.9-RC2</version>
</dependency>
并按如下方式修改配置。
@Configuration
public class RepositoryConfig extends SpringBootRepositoryRestMvcConfiguration {
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
//change com.yourpackage.domain to point to your domain classes
Reflections reflections = new Reflections("com.yourpackage.domain");
Set<Class<?>> entities = reflections.getTypesAnnotatedWith(Entity.class, false);
config..exposeIdsFor(entities.toArray(new Class[entities.size()]));
}
}