Java Jersey客户端在get请求时提供IllegalArgumentException?

Java Jersey客户端在get请求时提供IllegalArgumentException?,java,Java,我在尝试使用Jersey客户端发出get请求时遇到以下异常。如果我将requestPath发布到浏览器中,就会得到预期的Json响应 从浏览器访问requestPath时的结果: {“申请”:[ ],“错误”:0} Client=Client.create(); client.addFilter(新ClientFilter(){ @凌驾 公共ClientResponse句柄(ClientRequest请求)引发ClientHandlerException{ request.getHeaders(

我在尝试使用Jersey客户端发出get请求时遇到以下异常。如果我将
requestPath
发布到浏览器中,就会得到预期的Json响应

从浏览器访问
requestPath
时的结果:

{“申请”:[ ],“错误”:0}

Client=Client.create();
client.addFilter(新ClientFilter(){
@凌驾
公共ClientResponse句柄(ClientRequest请求)引发ClientHandlerException{
request.getHeaders().putSingle(HttpHeaders.CONTENT_类型,“application/json”);
返回getNext().handle(请求);
}
});
WebResource WebResource=client.resource(请求路径)
ClientResponse响应=webResource
.accept(“应用程序/json”)
.get(ClientResponse.class);
String responsest=response.getEntity(String.class);
泽西岛
泽西岛客户
1.18.1
泽西岛
泽西json
1.18.1
java.lang.IllegalArgumentException:解析媒体类型“application/json”时出错;字符集=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(本机方法)处
位于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.runReflectVeCall(FrameworkMethod.java:50)
位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
位于org.junit.runners.model.FrameworkMethod.invokeeexplosive(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)
原因:java.text.ParseException:不平衡的引号字符串
位于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个

服务器返回的内容类型标题已损坏:

java.lang.IllegalArgumentException: Error parsing media type 'application/json; charset=UTF-8"' 
...
Caused by: java.text.ParseException: Unbalanced quoted string
(注意悬挂

您可以解决此问题,如下所述:

更新

我设置了一个小测试,如下所示:

Client client = Client.create();
WebResource resource = client.resource("http://localhost:8080");
String response = resource.accept("application/json").get(String.class);
System.out.println(response);
在命令行上启动了一个小的“假”HTTP服务器,该服务器返回损坏的“内容类型”头

这导致了一个异常,看起来与您的异常非常相似:

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)
    ...
现在,我添加了一个过滤器来覆盖响应“内容类型”:

然后重新运行测试,瞧:

Hello

你能发布从浏览器收到的JSON响应吗?@javaguy当然,PostedTanks,我尝试覆盖ClientFilter,但我得到了相同的异常。我已经更新了我问题中的代码以包含我的覆盖。我相信您必须覆盖响应头,而不是像您一样覆盖请求头(在我提到的问题中有一些关于如何做到这一点的示例)。
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)
    ...
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;
    }
});
Hello