2015-12-09 157 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" 
    } 
}