405 尽管在使用 RepositoryRestConfiguration.disableDefaultExposure() 时公开了所有方法,但仍不允许方法

jeb*_*nch 5 java spring spring-data-jpa kotlin spring-data-rest

我不想默认公开我的存储库,它看起来RepositoryRestConfiguration.disableDefaultExposure()正是我想要的;但是我在调​​用时收到 405 响应/{respository/{id}

我已将存储库中的所有方法标记为公开的,并且据我所知,除FindById.

@RepositoryRestResource(exported = true)
interface MyEntityRepository : PagingAndSortingRepository<MyEntity, Int> {

    @RestResource(exported = true)
    override fun findAll(sort: Sort): MutableIterable<MyEntity>

    @RestResource(exported = true)
    override fun findAll(pageable: Pageable): Page<MyEntity>

    @RestResource(exported = true)
    override fun <S : MyEntity?> save(entity: S): S

    @RestResource(exported = true)
    override fun findAll(): MutableIterable<MyEntity>

    @RestResource(exported = true)
    override fun deleteById(id: Int)

    @RestResource(exported = true)
    override fun deleteAll(entities: MutableIterable<MyEntity>)

    @RestResource(exported = true)
    override fun deleteAll()

    @RestResource(exported = true)
    override fun <S : MyEntity?> saveAll(entities: MutableIterable<S>): MutableIterable<S>

    @RestResource(exported = true)
    override fun count(): Long

    @RestResource(exported = true)
    override fun findAllById(ids: MutableIterable<Int>): MutableIterable<MyEntity>

    @RestResource(exported = true)
    override fun delete(entity: MyEntity) 

    @RestResource(exported = true)
    override fun existsById(id: Int): Boolean

    @RestResource(exported = true)
    override fun findById(id: Int): Optional<MyEntity>

}

@Component
class SpringDataRestCustomization() : RepositoryRestConfigurerAdapter() {

    override fun configureRepositoryRestConfiguration(config: RepositoryRestConfiguration?) {
        config!!.disableDefaultExposure()
    }
}
Run Code Online (Sandbox Code Playgroud)

结果是:

GET /myentities/1
cache-control: no-cache
postman-token: 3d310c2e-2e1c-4587-880d-cc2f79cbb9eb
user-agent: PostmanRuntime/7.2.0
accept: */*
host: localhost:8080
accept-encoding: gzip, deflate

HTTP/1.1 405
status: 405
allow: PUT,PATCH,DELETE,OPTIONS
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
cache-control: no-cache, no-store, max-age=0, must-revalidate
pragma: no-cache
expires: 0
x-frame-options: DENY
content-length: 0
date: Fri, 27 Jul 2018 11:42:25 GMT
Run Code Online (Sandbox Code Playgroud)

使用 Spring Boot 并将spring-data-releasetrain.version其设置为Kay-SR9.

是否有我错过的方法或我需要执行其他操作才能允许 GET?我没有这个(或任何)存储库的控制器。

- - 编辑 - -

经过一番研究后,我想我已经找到了原因,但我不太喜欢我的解决方案;当Spring寻找方法时,findById它正在寻找类型的参数java.lang.Integer,但我的方法被定义为primitive int;Kotlin 将 ID 类型参数设置为 ,java.lang.Integer但将函数参数设置为int,因此它们不匹配。

按如下方式更新我的存储库类可以工作,但会导致Warning:(13, 67) Kotlin: This class shouldn't be used in Kotlin. Use kotlin.Int instead.

@RepositoryRestResource(exported = true)
interface MyEntityRepository : PagingAndSortingRepository<MyEntity, java.lang.Integer> {

    ...

    @RestResource(exported = true)
    override fun findById(id: java.lang.Integer): Optional<MyEntity>

}
Run Code Online (Sandbox Code Playgroud)

--- 编辑2 ---

我目前正在使用下面的代码,该代码有效;然而,Kotlin 认为我的方法不会覆盖继承的方法(如果我将overrides关键字添加回其中,它将无法编译)。

@RepositoryRestResource(exported = true)
interface MyEntityRepository : PagingAndSortingRepository<MyEntity, Int?> {

    ...

    @RestResource(exported = true)
    fun findById(id: Int?): Optional<MyEntity>

}
Run Code Online (Sandbox Code Playgroud)

Zac*_*ack 0

@jebbench 的编辑解决了GET /{entity}/{id}. 另外我必须这样做才能删除:

    @RestResource(exported = true)
    fun deleteById(id: Int?): Optional<MyEntity>
Run Code Online (Sandbox Code Playgroud)