2

我做了一个 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   

失败!/demo找不到 的 Grails 报告

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

我看不出有什么问题

4

1 回答 1

1

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

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

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

@ComponentScan("helloworld")
class Application extends GrailsAutoConfiguration {
  static void main(String[] args) {
    GrailsApp.run(Application)
  }
}
于 2015-04-26T08:52:32.583 回答