发现问题。毕竟,春季靴子的@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名称)不起作用。要解决此问题,需要@Autowired
到restTemplate
字段,并让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"
}
}
要么设置你的你的东西在resource.groovy或添加@ComponentScan(“包名”)在您的应用 – cfrick