grails-plugins / grails-rest-client-builder

REST client plugin that uses Spring's RestTemplate
http://grails.org/plugin/rest-client-builder
Apache License 2.0
65 stars 32 forks source link

How to call rest.get("some url") with header #36

Open wureka opened 9 years ago

wureka commented 9 years ago

Hi, My code is as below:

def getUser(String mid) {
    String HTTPS_URL_GET_USERS = "https://channel-apis.line.naver.jp/v1/profiles?mids=%s"
    def rest = new RestBuilder()
    log.debug("URL = " + String.format(HTTPS_URL_GET_USERS, mid))
    def resp = rest.get(String.format(HTTPS_URL_GET_USERS, mid)) {
        header 'X-Line-ChannelToken', LINE_ACCESS_TOKEN
        contentType "application/json; charset=UTF-8"
    }
    log.debug("user: ${resp.responseEntity.body}")
    def obj = grails.converters.JSON.parse(resp.responseEntity.body)
    return obj
}

When I call this method, the below exception occurs:

Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@3ac467f3; line: 1, column: 1]. Stacktrace follows:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@3ac467f3; line: 1, column: 1]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:749)
    at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:59)
    at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:12)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2206)
    at grails.plugins.rest.client.RestBuilder.invokeRestTemplate(RestBuilder.groovy:307)
    at grails.plugins.rest.client.RestBuilder.doRequestInternal(RestBuilder.groovy:280)
    at grails.plugins.rest.client.RestBuilder.doRequestInternal(RestBuilder.groovy:268)
    at grails.plugins.rest.client.RestBuilder.get(RestBuilder.groovy:113)
    at com.fet.ebu.imsghub.service.LineService$$EP6OF3Sl.$tt__getUser(LineService.groovy:104)
    at com.fet.ebu.imsghub.v1.LineController.index(LineController.groovy:20)
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

Would you please help me to solve the problem ? Thank you

atapin commented 9 years ago

@wureka I worked it around with adding accept JSONObject into the builder customizer. After that I can read resp.body as a JSONObject. Working on deserializing response to prepared POGOs now.