在通过REST-API获取数据时出现关于java.lang.IllegalArgumentException的问题

时间:2016-11-30 12:36:46

标签: java rest url

我尝试使用REST API并尝试连接到服务器并使用Java获取数据。 我开发了一些类似的代码;

Client client = Client.create();
WebResource webResource = client.resource("https://XXXX/0/devices/2c5a7b88-1eca-48b3-ae68-cdcb011a84d9/s/car-exists\\?latest=2");
WebResource.Builder builder = webResource.getRequestBuilder();
for (NewCookie c : cookies)
  builder = builder.cookie(c);
ClientResponse response = builder.accept("application/json").get(ClientResponse.class);

我在上面的最后一句之后得到一个例外;

javax.faces.FacesException: #{XXX}: java.lang.IllegalArgumentException: Illegal character in path at index 117: https://XXX/0/devices/2c5a7b88-1eca-48b3-ae68-cdcb011a84d9/s/car-exists\?latest=2
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    at org.primefaces.application.DialogActionListener.processAction(DialogActionListener.java:45)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at filter.NoCacheFilter.doFilter(NoCacheFilter.java:46)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.faces.el.EvaluationException: java.lang.IllegalArgumentException: Illegal character in path at index 117: https://XXX/0/devices/2c5a7b88-1eca-48b3-ae68-cdcb011a84d9/s/car-exists\?latest=2
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 27 more

我在命令行上使用了对REST-API有用的应用程序“curl”,并使用相同的URL轻松获取数据。但是,我无法在Java上做到这一点。 URL上有任何问题吗?请纠正我,因为我认为该代码没有错。

1 个答案:

答案 0 :(得分:3)

从网址中删除\\。它应该不存在。仅使用?启动查询字符串。