gpt4 book ai didi

带有 Spring Boot 自动配置的 Grails 3

转载 作者:行者123 更新时间:2023-12-02 14:41:11 25 4
gpt4 key购买 nike

我正在尝试将 Grails 应用程序与 Netflix Eureka 的东西集成,以便使用 Spring Cloud Ribbon 对服务进行 REST 调用。在普通的 Spring Boot 应用程序中,它只不过是添加所需的依赖项,而 Spring Boot 自动配置将确保我的 RestTemplate 配置为供 Ribbon 使用。

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

最佳答案

发现了问题。 Spring Boot 的@AutoConfigure毕竟在工作。

尝试使用 Spring 时出现问题 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 注册了启用的 Ribbon RestTemplate bean 不在 bean 名称下 restTemplate ,基于 Grails 约定的注入(inject)机制(字段名称必须与 bean 名称匹配)不起作用。要解决此问题,需要 @AutowiredrestTemplate字段并让 Spring 进行注入(inject)。

所以这是解决方案:
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"
}
}

关于带有 Spring Boot 自动配置的 Grails 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34179910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com