grails-http-builder-helper icon indicating copy to clipboard operation
grails-http-builder-helper copied to clipboard

groovy.lang.MissingMethodException when calling withHttp() from a service

Open ghost opened this issue 9 years ago • 1 comments

Service class:

class GeoCoderService {
    def getLatLong(String address) {
        def result = null
        withHttp(uri: "http://maps.googleapis.com") {
            def html = get(path : '/maps/api/geocode/json', query : [address:address])
            if ( html.results != null && html.results.size() > 0 ) {
                def partial = html.results[0]
                if ( partial.geometry != null && partial.geometry.location != null) {
                    def lat = partial.geometry.location.lat
                    def lng = partial.geometry.location.lng
                    result = [lat:Double.valueOf(lat.toString()), lng:Double.valueOf(lng.toString())]
                }
            }
        }
        return result
    }
}

Controller class:

class GeneralController {

    def geoCoderService

    def index = {
        render (view: "index")
    }

    def test = {
        def point = geoCoderService.getLatLong('1600 Amphitheatre Pkwy, Mountain View, CA 94043')
        render "latitude = ${point.lat}"
        render "longitude = ${point.lng}"
    }
}

Exception:

Caused by: groovy.lang.MissingMethodException: No signature of method: package.GeoCoderService.withHttp() is applicable for argument types: (java.util.LinkedHashMap, package.GeoCoderService$_getLatLong_closure1) values: [[uri:http://maps.googleapis.com], package.GeoCoderService$_getLatLong_closure1@5ad666a2]
        at package.GeoCoderService.getLatLong(GeoCoderService.groovy:6) ~[main/:na]
        at package.GeneralController$_closure2.doCall(GeneralController.groovy:12) ~[main/:na]
        ... 3 common frames omitted

ghost avatar Sep 11 '15 15:09 ghost

Add above lines at top import groovyx.net.http.HTTPBuilder

sudhanshusirohia avatar Nov 17 '15 11:11 sudhanshusirohia