gpt4 book ai didi

java - Jersey 客户端在获取请求时给出 IllegalArgumentException?

转载 作者:行者123 更新时间:2023-11-30 02:44:44 25 4
gpt4 key购买 nike

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

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

{ "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

最佳答案

服务器返回的 Content-Type header 已损坏:

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

(注意悬空")

您可以按照此处所述解决此问题:How to override response header in jersey client

更新

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

在命令行上启动了一个小型“假”HTTP 服务器,该服务器返回损坏的“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

尝试使用 Jersey Client 访问它,如下:

Client client = Client.create();
WebResource resource = client.resource("http://localhost:8080");
String response = resource.accept("application/json").get(String.class);
System.out.println(response);

这导致了一个异常,看起来很像你所遇到的异常:

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)
...

现在,我添加了一个过滤器来覆盖响应“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;
}
});

然后重新运行测试,瞧:

Hello

关于java - Jersey 客户端在获取请求时给出 IllegalArgumentException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40536755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com