简体   繁体   中英

Jersey Client gives IllegalArgumentException on get request?

I am getting the below exception trying to make a get request using a Jersey Client. If I post requestPath into my browser, I get the expected Json response.

Result when I visit requestPath from my 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"'

    at com.sun.jersey.core.impl.provider.header.MediaTypeProvider.fromString(MediaTypeProvider.java:79)
    at com.sun.jersey.core.impl.provider.header.MediaTypeProvider.fromString(MediaTypeProvider.java:53)
    at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)
    at com.sun.jersey.api.client.ClientResponse.getType(ClientResponse.java:695)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:612)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:586)
    at MyTest.test(MyTest.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
Caused by: java.text.ParseException: Unbalanced quoted string
    at com.sun.jersey.core.header.reader.HttpHeaderReaderImpl.processQuotedString(HttpHeaderReaderImpl.java:313)
    at com.sun.jersey.core.header.reader.HttpHeaderReaderImpl.process(HttpHeaderReaderImpl.java:243)
    at com.sun.jersey.core.header.reader.HttpHeaderReaderImpl.next(HttpHeaderReaderImpl.java:184)
    at com.sun.jersey.core.header.reader.HttpHeaderReader.nextSeparator(HttpHeaderReader.java:112)
    at com.sun.jersey.core.header.reader.HttpHeaderReader.readParameters(HttpHeaderReader.java:239)
    at com.sun.jersey.core.impl.provider.header.MediaTypeProvider.valueOf(MediaTypeProvider.java:97)
    at com.sun.jersey.core.impl.provider.header.MediaTypeProvider.fromString(MediaTypeProvider.java:77)
    ... 28 more

The Content-Type header, which server returns, is 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 this like described here: How to override response header in jersey client

UPDATE

I've setup a small test as follows:

Launched a small "fake" HTTP server on the command line, which returns the broken "Content-Type" header

while true 
do 
cat<<EOF | nc -l 8080
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8"

Hello
EOF 
done

Tried to access it with Jersey Client, as 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 an Exception, looking pretty much one you have:

Exception in thread "main" java.lang.IllegalArgumentException: Error parsing media type 'application/json; charset=UTF-8"\r\n'
    at com.sun.jersey.core.impl.provider.header.MediaTypeProvider.fromString(MediaTypeProvider.java:79)
    at com.sun.jersey.core.impl.provider.header.MediaTypeProvider.fromString(MediaTypeProvider.java:53)
    at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)
    ...
Caused by: java.text.ParseException: Unbalanced quoted string
    at com.sun.jersey.core.header.reader.HttpHeaderReaderImpl.processQuotedString(HttpHeaderReaderImpl.java:263)
    at com.sun.jersey.core.header.reader.HttpHeaderReaderImpl.process(HttpHeaderReaderImpl.java:192)
    ...

Now, I've added a filter to override the 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 then re-run the test and voila:

Hello

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM