Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This annoyed me! Expected a string but was BEGIN_OBJECT at line 1 column 2. #40

Open
biniama opened this issue Jul 9, 2015 · 18 comments
Labels

Comments

@biniama
Copy link

biniama commented Jul 9, 2015

Error 2015-07-09 13:30:22,849 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver - IllegalStateException occurred when processing request: [GET] /recipes-mashup/bigOven/batch
Expected a string but was BEGIN_OBJECT at line 1 column 2. Stacktrace follows:
Message: Expected a string but was BEGIN_OBJECT at line 1 column 2
Line | Method
->> 821 | nextString in com.google.gson.stream.JsonReader


| 358 | read in com.google.gson.internal.bind.TypeAdapters$13
| 346 | read . . . . . . . in ''
| 803 | fromJson in com.google.gson.Gson
| 768 | fromJson . . . . . in ''
| 312 | invokeRestTemplate in grails.plugins.rest.client.RestBuilder
| 273 | doRequestInternal in ''
| 261 | doRequestInternal in ''
| 106 | get . . . . . . . in ''
| 29 | getBatch in com.bonial.hackathon.services.BigOvenService
| 16 | batch . . . . . . in com.bonial.hackathon.BigOvenController
| 198 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter . . . . . in grails.plugin.cache.web.filter.AbstractFilter
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run in java.lang.Thread

@graemerocher
Copy link
Member

Great that it annoyed you, but please provide more information than this, we cannot possibly reproduce if you just paste a stack trace

@biniama
Copy link
Author

biniama commented Jul 10, 2015

This is the code I tried it with.

    String api_key = "123"

    RestBuilder rest = new RestBuilder()

    def resp = rest.get('http://api.bigoven.com/recipes?pg=1&rpp=9&title_kw=cake&api_key=$api_key')   {
        accept 'application/json'
        contentType 'application/json'
    }

    render resp.json

I found a workaround,
/** WORKAROUND
*
* error:
* "org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2;
*
* found from: https://jira.grails.org/browse/GPREST-13
* */
rest.restTemplate.messageConverters.removeAll {
it.class.name == 'org.springframework.http.converter.json.GsonHttpMessageConverter'
}

But, I still need a solution.

Plus, the returned value's content type is not 'application/json' rather 'text/html;charset=utf-8'

@pablovilas
Copy link

I've the same problem, any idea?

@pablovilas
Copy link

Well, looking at the code I've found a solution

def resp = rest.get('http://api.bigoven.com/recipes?pg=1&rpp=9&title_kw=cake&api_key=$api_key') {
contentType 'application/json'
accept JsonObject
}

You must replace 'application/json' with JsonObject, I think that both ways should be supported.
Good luck

@matthijsbierman
Copy link

For the old behaviour, which will parse it into a org.codehaus.groovy.grails.web.json.JSONObject use:

import org.codehaus.groovy.grails.web.json.JSONObject
...
rest.post(url) {
    accept JSONObject, 'application/json'
}

@bobbywarner
Copy link

This works just fine for me:

accept("application/json")

So, not sure how to replicate. Can you please post a sample app that demonstrates the problem?

@matthijsbierman
Copy link

Sure. The problem may be related to the fact that I'm unit testing with MockRestServiceServer. If you create a new app with Grails 2.5.1:

grails create-app app

And then overwrite/add these files: https://dl.dropboxusercontent.com/u/55056243/example.tar.gz. Update dependencies.

In TestService you can toggle between the two accept statements. Using accept 'application/json' will fail.

@bkoehm
Copy link

bkoehm commented Nov 26, 2015

I believe I have fixed this with the information contained in:
#44

@biniama
Copy link
Author

biniama commented Dec 9, 2015

@matthijsbierman 's solution worked. I am closing the issue.

@biniama biniama closed this as completed Dec 9, 2015
@matthijsbierman
Copy link

Glad that it helped, but I don't believe you should have closed the issue already. There is still a problem, because it should work out of the box. It should only be closed once the bug is fixed upstream, as @bkoehm mentioned.

@biniama
Copy link
Author

biniama commented Dec 10, 2015

I am re-openning. I spent time trying to cast the output to JSON and perform operations on the JSON but with out success.

@biniama biniama reopened this Dec 10, 2015
@graemerocher
Copy link
Member

Use the new version of the underlying JAR to solve the problem:

 compile 'org.grails:grails-datastore-rest-client:5.0.0.RC2'

@dustindclark
Copy link

@graemerocher, in Grails 2.5.3, updating the JAR results in this issue

ClassNotFoundException: org.grails.web.converters.configuration.ConvertersConfigurationHolder

I assume that's because the fix only applies to Grails 3.X. Any solution for 2.5.3 (short of refactoring a bunch of code per the workarounds above)?

@Omnipresent
Copy link

I'm having the same error on 2.5.2 using 2.0.0 version of the plugin.

Below code reproduces the problem

            def resp = rest.post("https://www.google.com/recaptcha/api/siteverify") {
            accept("application/json")
            urlVariables secret:"asdfsdf", response: "lkjklj"
        }

@dustindclark
Copy link

@Omnipresent, the workaround mentioned above works on 2.5.x. Here's my snippet:

RestBuilder rest = new RestBuilder()
//Workaround per https://github.com/grails-plugins/grails-rest-client-builder/issues/40
rest.restTemplate.messageConverters.removeAll {
     it.class.name == 'org.springframework.http.converter.json.GsonHttpMessageConverter'
}
rest.post...

@Omnipresent
Copy link

@dustindclark Thanks, that works.

For completeness sake

        RestBuilder rest = new RestBuilder()
        rest.restTemplate.messageConverters.removeAll {
            it.class.name == 'org.springframework.http.converter.json.GsonHttpMessageConverter'
        }
        def resp = rest.post("https://www.google.com/recaptcha/api/siteverify?secret=df&response="+params["g-recaptcha-response"])
        def json = resp.json

wijnand1 referenced this issue in grails/grails-data-mapping Mar 8, 2016
Remove org.springframework.http.converter.json.GsonHttpMessageConverter.
@wijnand1
Copy link

wijnand1 commented Mar 8, 2016

For others having this problem in grails 2.5.3: this workaround can be generalized by overwriting the default constructor for the RestBuilder.
In Bootstrap.groovy include this:

RestBuilder.metaClass.constructor = { ->
    def constructor = RestBuilder.class.getConstructor()
    def instance = constructor.newInstance()
    instance.restTemplate.messageConverters.removeAll { it.class.name == 'org.springframework.http.converter.json.GsonHttpMessageConverter' }
    instance
}

@tservin
Copy link

tservin commented Jan 30, 2019

hi
im working on observe 5.4 data base but my java keep going into error.
java.lang.illegalState Exeption:Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
can any oone help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants