Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何禁用RepositoryRestHandlerMapping和EndpointHandlerMapping?_Spring_Spring Mvc_Spring Boot_Spring Data Jpa_Spring Hateoas - Fatal编程技术网

Spring 如何禁用RepositoryRestHandlerMapping和EndpointHandlerMapping?

Spring 如何禁用RepositoryRestHandlerMapping和EndpointHandlerMapping?,spring,spring-mvc,spring-boot,spring-data-jpa,spring-hateoas,Spring,Spring Mvc,Spring Boot,Spring Data Jpa,Spring Hateoas,我目前正在使用SpringBoot、Hibernate和SpringHateoas构建一个具有REST接口的应用程序。我的数据模型被定义为带有@Entity注释的bean,我正在使用Spring的功能自动设置Hibernate存储库(创建一个扩展分页和排序存储库的接口)。我的应用程序是完全由注释驱动的,也就是说,我没有web.xml,但使用Spring注释配置一切,如@Configuration,@Bean等,并在SpringApplication.run(MyApp.class,args)的帮

我目前正在使用SpringBoot、Hibernate和SpringHateoas构建一个具有REST接口的应用程序。我的数据模型被定义为带有
@Entity
注释的bean,我正在使用Spring的功能自动设置Hibernate存储库(创建一个扩展
分页和排序存储库的接口)。我的应用程序是完全由注释驱动的,也就是说,我没有
web.xml
,但使用Spring注释配置一切,如
@Configuration
@Bean
等,并在
SpringApplication.run(MyApp.class,args)的帮助下从我的
main
方法启动应用程序


这很好,但使用这种方法,将创建
RepositoryRestHandlerMapping
EndpointHandlerMapping
。这些创造了一堆我既不需要也不想要的资源。我实现自己的控制器,因为它们需要做的不仅仅是标准逻辑


如何防止这种默认行为并禁用这些映射?

我需要其他REST函数,如
@RestController
注释。但我现在自己找到了一个可行的解决方案:


不应禁用RepositoryRestHandlerMapping
,但可以通过使用
@RepositoryRestResource(exported=false)
对存储库进行注释来禁用存储库的导出。我在我所有的存储库中都这样做了,现在,通配符资源仍然安装,但是没有注册存储库来解决它们,使它们实际上消失了。尝试访问这样的资源会产生预期的
404

EndpointHandlerMapping
相同,它来自
spring boot actuator
,安装了一些端点,如
/info
/metrics
等。这很方便,应该出现在REST应用程序中;当我向Eureka服务器注册我的应用程序时,它会自动生成指向其中一些应用程序的链接。要正确使用此功能,可以通过
@Bean
配置端点,例如:

@Configuration
public class InfoConfiguration {

    @Bean
    public InfoEndpoint infoEndpoint {
        Map<String, Object> info = ...
        return new InfoEndpoint(info);
    }
}
@配置
公共类信息配置{
@豆子
公共信息端点信息端点{
地图信息=。。。
返回新的InfoEndpoint(info);
}
}

上面的
info
是常量信息,如果有需要更改的信息,可以重写
InfoEndpoint
,并在主类中提供
getAdditionalInfo()
排除RepositoryRestMvcAutoConfiguration的自定义实现

@EnableAutoConfiguration(exclude = RepositoryRestMvcAutoConfiguration.class)
Kotlin
  • 排除特定资源:要仅排除特定存储库,请在特定界面中使用下面的代码,控制器中的映射仍然有效

    @Repository
    @RestResource(exported = false)
    interface SongRepository : JpaRepository<Song, Int>
    
使用下面的依赖项

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

org.springframework.boot
SpringBootStarterWeb
而不是

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

org.springframework.boot
弹簧启动启动器数据rest

当添加到依赖项下面时,HAL资源的自动创建也会立即出现

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

org.springframework.data
spring数据资源管理器
正如依赖项名称所说,它会自动为您创建HAL浏览器链接。

如果不希望自动创建控制器,请删除此依赖项。

RepositoryRestHandlerMapping
由Spring Data REST提供。如果您不想要它,那么就不要包含它。它也是这样工作的:
@springbootcapplication(exclude=RepositoryRestMvcAutoConfiguration.class)
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-hal-explorer</artifactId>
        </dependency>