【问题标题】:spring boot RestController not seen from Grails从 Grails 看不到 spring boot RestController
【发布时间】:2015-07-04 16:48:50
【问题描述】:

我已经做了一个 grails 3 应用程序

$ grails create-app helloworld  

在这个中我创建了一个弹簧控制器 DemoController.groovy

package helloworld
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
/**
 * A controller responding to
 * curl http://localhost:8080/demo
 * source location: src/main/groovy/helloworld/DemoController.groovy
 *
 */
@RestController
class DemoController {
  @RequestMapping("/demo")
  String demo() {
    "Hello demo!"
  }
}

使用

$spring run src/main/groovy/helloworld/DemoController.groovy &  
$curl http://localhost:8080/demo   

工作正常

使用

$gradle bootrun &  
$curl http://localhost:8080/demo   

失败! Grails 报告无法找到 /demo

当我从一个干净的 spring boot 应用程序(由 http://start.spring.io/ 制作)做同样的事情时,它工作正常

我看不出有什么问题

【问题讨论】:

  • 你必须将你的spring控制器定义为bean。您可以在 resources.groovy beans 闭包 (demoController(DemoController)) 中定义它,或将控制器包添加到 grails.spring.bean.packages 配置属性或使用 @ComponentScan 注释您的 Application

标签: spring spring-mvc grails spring-boot grails-3.0


【解决方案1】:

我在 (Grails 3 and Spring @RequestMapping) 中找到了解决方案

Grails 应用程序必须对 Grails 应用程序 java 包的包层次结构有一个 @ComponentScan。在这种情况下,它是“helloworld”

Application.groovy(由 Grails 生成)现在看起来像这样:

@ComponentScan("helloworld")
class Application extends GrailsAutoConfiguration {
  static void main(String[] args) {
    GrailsApp.run(Application)
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    • 2018-08-07
    • 1970-01-01
    相关资源
    最近更新 更多