2015-12-09 124 views
0

我正试图集成Grails应用程序与Netflix Eureka东西使用Spring云功能区为REST调用服务。在普通的Spring Boot应用程序中,只需添加所需的依赖关系即可,并且spring boot autoconfigure将确保我的RestTemplate已配置为使用功能区。Grails 3与弹簧引导自动配置

但是在我们的Grails(3.0.7)应用程序中,Spring Boot自动配置不会启动。有没有人有想过让Spring Boot autoconfigure工作的Grails?

+0

要么设置你的你的东西在resource.groovy或添加@ComponentScan(“包名”)在您的应用 – cfrick

回答

0

发现问题。毕竟,春季靴子的@AutoConfigure正在工作。

class MyController { 

    RestTemplate restTemplate 

    def index() { 
     def result = restTemplate.getEntity("http://my-service/whatever", Void.class) // call gives nullPointerException due restTemplate is not injected 
     render "Response: $result" 
    } 
} 

因为Spring引导注册启用丝带RestTemplate豆不在bean名称restTemplate,Grails的约定基于注射机构(字段名称必须匹配:试图用一个弹簧RestTemplate休息丝带时

问题bean名称)不起作用。要解决此问题,需要@AutowiredrestTemplate字段,并让Spring进行注入。

所以这是解决方案:

class MyController { 

    @AutoWired 
    RestTemplate restTemplate 

    def index() { 
     def result = restTemplate.getEntity("http://my-service/whatever", Void.class) // restTemplate is now injected using Spring instead of Grails 
     render "Response: $result" 
    } 
}