Open biniama opened 9 years ago
Great that it annoyed you, but please provide more information than this, we cannot possibly reproduce if you just paste a stack trace
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'
I've the same problem, any idea?
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
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'
}
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?
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.
I believe I have fixed this with the information contained in: https://github.com/grails-plugins/grails-rest-client-builder/issues/44
@matthijsbierman 's solution worked. I am closing the issue.
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.
I am re-openning. I spent time trying to cast the output to JSON and perform operations on the JSON but with out success.
Use the new version of the underlying JAR to solve the problem:
compile 'org.grails:grails-datastore-rest-client:5.0.0.RC2'
@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)?
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"
}
@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...
@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
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
}
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
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