How to disable RepositoryRestHandlerMapping and EndpointHandlerMapping?
Asked Answered
I

5

21

I am currently building an application with a REST interface, using Spring Boot, Hibernate and Spring-HATEOAS. My data model is defined as beans with @Entity annotation and I am using Spring's feature to automatically set up a Hibernate repository (Creating an interface extending PagingAndSortingRepository). My application is completely annotation-driven, i.e., I have no web.xml but configure everything with the Spring annotations like @Configuration, @Bean etc., and start the application from my main method with the help of SpringApplication.run(MyApp.class, args);

This works fine, but with this approach, a RepositoryRestHandlerMapping and EndpointHandlerMapping is created. These create a bunch of resources I neither need nor want. I implement my own controllers because they need to do more than the standard logic.

How can I prevent this default behaviour and disable these mappings?

Interpretative answered 4/11, 2014 at 15:49 Comment(1)
RepositoryRestHandlerMapping is provided by Spring Data REST. If you don't want it then don't include it.Aviv
I
9

I need the other REST functions, like @RestController annotation. But I found a feasible solution myself now:

RepositoryRestHandlerMapping should not be disabled, but it is possible to disable exporting of repositories by annotating them with @RepositoryRestResource(exported = false). I did this with all my repositories and now, the wildcard resources are still installed, but no repositories are registered to resolve against them, making them effectively disappear. Trying to access such a resource gives a 404 as expected.

Same for EndpointHandlerMapping, which comes from spring-boot-actuator and installs some endpoints like /info, /metrics etc. This is handy and should be present in a REST application; when I register my application with an Eureka server, it automatically generates links to some of these. To use this correctly, the endpoints can for example be configured via @Bean, like this:

@Configuration
public class InfoConfiguration {

    @Bean
    public InfoEndpoint infoEndpoint {
        Map<String, Object> info = ...
        return new InfoEndpoint(info);
    }
}

The info above is constant info, if there were info which is subject to change, one could override InfoEndpoint and supply a custom implementation of getAdditionalInfo().

Interpretative answered 4/11, 2014 at 16:41 Comment(0)
R
27

Exclude RepositoryRestMvcAutoConfiguration in your main class.

@EnableAutoConfiguration(exclude = RepositoryRestMvcAutoConfiguration.class)
Range answered 13/10, 2015 at 12:47 Comment(1)
It also works in this way: @SpringBootApplication(exclude = RepositoryRestMvcAutoConfiguration.class)Lunetta
I
9

I need the other REST functions, like @RestController annotation. But I found a feasible solution myself now:

RepositoryRestHandlerMapping should not be disabled, but it is possible to disable exporting of repositories by annotating them with @RepositoryRestResource(exported = false). I did this with all my repositories and now, the wildcard resources are still installed, but no repositories are registered to resolve against them, making them effectively disappear. Trying to access such a resource gives a 404 as expected.

Same for EndpointHandlerMapping, which comes from spring-boot-actuator and installs some endpoints like /info, /metrics etc. This is handy and should be present in a REST application; when I register my application with an Eureka server, it automatically generates links to some of these. To use this correctly, the endpoints can for example be configured via @Bean, like this:

@Configuration
public class InfoConfiguration {

    @Bean
    public InfoEndpoint infoEndpoint {
        Map<String, Object> info = ...
        return new InfoEndpoint(info);
    }
}

The info above is constant info, if there were info which is subject to change, one could override InfoEndpoint and supply a custom implementation of getAdditionalInfo().

Interpretative answered 4/11, 2014 at 16:41 Comment(0)
M
4

Kotlin

  • Exclude Specific resource: To exclude only a specific Repository use the code below in the specific interface, the mapping in the Controller will still work.

    @Repository
    @RestResource(exported = false)
    interface SongRepository : JpaRepository<Song, Int>
    
  • Entirely: To exclude entirely, use the Kotlin version of the previous answers in the main class:

     @SpringBootApplication
     @EnableAutoConfiguration(exclude = arrayOf(RepositoryRestMvcAutoConfiguration::class))
     class WebserviceApplication
    
Magpie answered 9/10, 2017 at 14:33 Comment(0)
B
1

use below dependency

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>

instead of

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
Brabant answered 13/6, 2019 at 9:26 Comment(0)
B
0

The auto creation of HAL resources also comes out of the box when added below dependency.

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-hal-explorer</artifactId>
        </dependency>

As the dependency name says, it creates HAL explorer links for you, automatically.
Delete this dependency if you don't want auto creation of controllers.

Bookmark answered 11/2, 2021 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.