【问题标题】:@RepositoryRestController - doesn't work in Spring boot 2 (Spring data rest)@RepositoryRestController - 在 Spring boot 2 中不起作用(Spring 数据休息)
【发布时间】:2018-08-28 07:36:24
【问题描述】:

我正在使用 SPRING DATA REST 。我有这个控制器。

org.springframework.data.rest.webmvc.RepositoryRestController
import org.springframework.http.HttpMethod
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Component
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestController
@RepositoryRestController
class UserController {

    @RequestMapping("/testing")
    fun secured(): String {
        return "Hello world"
    }

    @RequestMapping(method = arrayOf(RequestMethod.GET), value = "/scanners")
    @ResponseBody
    fun getProducers(): ResponseEntity<*> {

        return ResponseEntity.ok<Any>("this is just a tedskljdksjdksjkdsjdkskdst")
    }
}

我无法访问此端点,我总是得到 404 NOT FOUND,当我更改为 @RestController 时,我可以点击路线。

【问题讨论】:

  • 我认为当您没有在配置主类应用程序中使用@ComponentScan(basePackages = 正确包含控制器时,就会出现这种情况。你能检查一下吗?

标签: spring spring-mvc spring-boot spring-data spring-data-jpa


【解决方案1】:

我找到了解决方案。如果您有用户实体,并且您的端点默认为 /users,如果您需要创建新端点,则必须将其命名为 /users/testing,以这种方式您不会收到 404 错误。

@RepositoryRestController
class UserController {

    @RequestMapping("/users/testing")
    fun secured(): String {
        return "Hello world"
    }

    @RequestMapping(method = arrayOf(RequestMethod.GET), value = "/scanners")
    @ResponseBody
    fun getProducers(): ResponseEntity<*> {

        return ResponseEntity.ok<Any>("this is just a test")
    }
}

【讨论】:

    猜你喜欢
    • 2016-08-07
    • 2018-06-03
    • 2021-10-28
    • 1970-01-01
    • 2016-08-06
    • 2019-07-10
    • 1970-01-01
    • 2017-04-13
    相关资源
    最近更新 更多