I'm trying to integrate a Grails application with the Netflix Eureka stuff for using Spring Cloud Ribbon for making REST calls to services. In a normal Spring Boot application it is nothing more than adding the required dependencies and the spring boot autoconfigure will make sure that my RestTemplate is configured for the Ribbon use.
But in our Grails (3.0.7) application the Spring Boot autoconfiguration will not kick in. Does anyone have an idea to get the Grails with Spring Boot autoconfigure working?
Found the problem. Spring boot's @AutoConfigure
was working after all.
Problem when trying to use a Spring RestTemplate
for rest with Ribbon:
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"
}
}
Because Spring Boot registers the Ribbon enabled RestTemplate
bean not under bean name restTemplate
, the Grails convention based injection mechanism (field name must match bean name) doesn't work. To work around this problem it is needed to @Autowired
to the restTemplate
field and let Spring do the injection.
So this is the solution:
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"
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.