java - Jersey Client gives IllegalArgumentException on get request? -
i getting below exception trying make request using jersey client. if post requestpath browser, expected json response.
result when visit requestpath browser:
{ "application":[ ], "error":0 }
client client = client.create(); client.addfilter(new clientfilter() { @override public clientresponse handle(clientrequest request) throws clienthandlerexception { request.getheaders().putsingle(httpheaders.content_type, "application/json"); return getnext().handle(request); } }); webresource webresource = client.resource(requestpath) clientresponse response = webresource .accept("application/json") .get(clientresponse.class); string responsestr = response.getentity(string.class); <dependency> <groupid>com.sun.jersey</groupid> <artifactid>jersey-client</artifactid> <version>1.18.1</version> </dependency> <dependency> <groupid>com.sun.jersey</groupid> <artifactid>jersey-json</artifactid> <version>1.18.1</version> </dependency> java.lang.illegalargumentexception: error parsing media type 'application/json; charset=utf-8"' @ com.sun.jersey.core.impl.provider.header.mediatypeprovider.fromstring(mediatypeprovider.java:79) @ com.sun.jersey.core.impl.provider.header.mediatypeprovider.fromstring(mediatypeprovider.java:53) @ javax.ws.rs.core.mediatype.valueof(mediatype.java:119) @ com.sun.jersey.api.client.clientresponse.gettype(clientresponse.java:695) @ com.sun.jersey.api.client.clientresponse.getentity(clientresponse.java:612) @ com.sun.jersey.api.client.clientresponse.getentity(clientresponse.java:586) @ mytest.test(mytest.java:31) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:50) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:47) @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:17) @ org.junit.runners.parentrunner.runleaf(parentrunner.java:325) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:78) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:57) @ org.junit.runners.parentrunner$3.run(parentrunner.java:290) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288) @ org.junit.runners.parentrunner.access$000(parentrunner.java:58) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.junit.runner.junitcore.run(junitcore.java:137) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:117) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:42) @ com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:262) @ com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:84) caused by: java.text.parseexception: unbalanced quoted string @ com.sun.jersey.core.header.reader.httpheaderreaderimpl.processquotedstring(httpheaderreaderimpl.java:313) @ com.sun.jersey.core.header.reader.httpheaderreaderimpl.process(httpheaderreaderimpl.java:243) @ com.sun.jersey.core.header.reader.httpheaderreaderimpl.next(httpheaderreaderimpl.java:184) @ com.sun.jersey.core.header.reader.httpheaderreader.nextseparator(httpheaderreader.java:112) @ com.sun.jersey.core.header.reader.httpheaderreader.readparameters(httpheaderreader.java:239) @ com.sun.jersey.core.impl.provider.header.mediatypeprovider.valueof(mediatypeprovider.java:97) @ com.sun.jersey.core.impl.provider.header.mediatypeprovider.fromstring(mediatypeprovider.java:77) ... 28 more
the content-type header, server returns, broken:
java.lang.illegalargumentexception: error parsing media type 'application/json; charset=utf-8"' ... caused by: java.text.parseexception: unbalanced quoted string (note dangling ")
you can workaround described here: how override response header in jersey client
update
i've setup small test follows:
launched small "fake" http server on command line, returns broken "content-type" header
while true cat<<eof | nc -l 8080 http/1.1 200 ok content-type: application/json; charset=utf-8" hello eof done tried access jersey client, follows:
client client = client.create(); webresource resource = client.resource("http://localhost:8080"); string response = resource.accept("application/json").get(string.class); system.out.println(response); that resulted in exception, looking pretty 1 have:
exception in thread "main" java.lang.illegalargumentexception: error parsing media type 'application/json; charset=utf-8"\r\n' @ com.sun.jersey.core.impl.provider.header.mediatypeprovider.fromstring(mediatypeprovider.java:79) @ com.sun.jersey.core.impl.provider.header.mediatypeprovider.fromstring(mediatypeprovider.java:53) @ javax.ws.rs.core.mediatype.valueof(mediatype.java:119) ... caused by: java.text.parseexception: unbalanced quoted string @ com.sun.jersey.core.header.reader.httpheaderreaderimpl.processquotedstring(httpheaderreaderimpl.java:263) @ com.sun.jersey.core.header.reader.httpheaderreaderimpl.process(httpheaderreaderimpl.java:192) ... now, i've added filter override response "content-type":
client.addfilter(new clientfilter() { @override public clientresponse handle(clientrequest request) throws clienthandlerexception { clientresponse response = getnext().handle(request); response.getheaders().put(httpheaders.content_type, arrays.aslist(mediatype.application_json)); return response; } }); and re-run test , voila:
hello
Comments
Post a Comment