从Grails看不到的Spring Boot RestController

时间:2015-04-26 08:19:40

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

我已经申请了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

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

我看不出有什么问题

1 个答案:

答案 0 :(得分:1)

我在(Grails 3 and Spring @RequestMapping

中找到了解决方案

Grails应用程序必须具有@ComponentScan到Grails应用程序java包的包层次结构。在这种情况下,它是" helloworld"

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

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