繁体   English   中英

具有Spring Boot自动配置功能的Grails 3

[英]Grails 3 with Spring boot autoconfigure

我正在尝试将Grails应用程序与Netflix Eureka集成在一起,以使用Spring Cloud Ribbon对服务进行REST调用。 在普通的Spring Boot应用程序中,无非就是添加必需的依赖项,并且Spring Boot自动配置将确保将我的RestTemplate配置为功能区使用。

但是在我们的Grails(3.0.7)应用程序中,Spring Boot自动配置将无法启动。是否有人有想法让Spring Boot自动配置正常运行的Grails?

找到了问题。 春季启动的@AutoConfigure在工作。

尝试使用Spring RestTemplate与功能区RestTemplate休息时出现问题:

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 Boot不在使用bean名称restTemplate下注册了启用了Ribbon的RestTemplate bean,所以基于Grails约定的注入机制(字段名称必须与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"
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM