【问题标题】:How to override class如何覆盖类
【发布时间】:2015-05-02 07:13:54
【问题描述】:

我正在使用最新版本的 spring-boot 并尝试将 id 添加到我的 json 实体中,如 here 所述

如果我添加此配置,我的 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);
        }
    }

我做错了什么?

【问题讨论】:

  • 在添加 RepositoryConfig 之前你的 baseuri 是如何配置的?

标签: spring-boot spring-data-rest


【解决方案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()]));
    }
}

【讨论】:

猜你喜欢
  • 2021-10-22
  • 2012-06-23
  • 2019-12-12
  • 2012-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-09
相关资源
最近更新 更多