jax
jax

Reputation: 38583

How to override class

I am using the latest version of spring-boot and am trying to add ids to my json entities as described here

If I add this configuration, my JSON responses no longer work:

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]

Here is my main configuration file, I have tried moving RepositoryConfig into it's own file and using @Component instead of @Configuration but nothing works.

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

What am I doing wrong?

Upvotes: 0

Views: 2415

Answers (1)

jax
jax

Reputation: 38583

I got this working, I needed to extend SpringBootRepositoryRestMvcConfiguration not RepositoryRestMvcConfiguration

@Configuration
public class RepositoryConfig extends
        SpringBootRepositoryRestMvcConfiguration {

    @Override
    protected void configureRepositoryRestConfiguration(
            RepositoryRestConfiguration config) {
        config.exposeIdsFor(Tutorial.class);
    }
}

If you want all your jpa entities to expose their id value, you can do the following

Add this dependency:

<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections-maven</artifactId>
    <version>0.9.9-RC2</version>
</dependency>

And modify the configuration as follows.

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

Upvotes: 1

Related Questions