调用Restful Webservice时,Uri不是绝对异常

时间:2013-02-13 07:58:13

标签: java web-services http servlets cas

以下代码段用于使用restful API调用我的Web服务。

ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    String uri= "https://127.0.0.1:8443/cas-server-webapp-3.5.0/login";
    WebResource resource = client.resource(URLEncoder.encode(uri));
      MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
       queryParams.add("username", "suresh");
       queryParams.add("password", "suresh");
       resource.queryParams(queryParams); 
       ClientResponse response = resource.type(
            "application/x-www-form-urlencoded").get(ClientResponse.class);
    String en = response.getEntity(String.class);
    System.out.println(en); 

运行上述代码时出现此异常

com.sun.jersey.api.client.ClientHandlerException: java.lang.IllegalArgumentException: URI is not absolute

    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:151)
    at com.sun.jersey.api.client.Client.handle(Client.java:648)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)

我搜索了许多文章,并没有得到我做错的地方。

旁注:我在Apache tomacat7的机器上部署了cas-server-webapp-3.5.0 war

5 个答案:

答案 0 :(得分:8)

绝对URI指定方案;一个非绝对的URI被认为是相对的。

http://docs.oracle.com/javase/8/docs/api/java/net/URI.html

那么,也许您的URLEncoder没有按预期工作(https位)?

    URLEncoder.encode(uri) 

答案 1 :(得分:4)

问题可能是你在已经是URI的东西上调用URLEncoder.encode()。

答案 2 :(得分:3)

对于其他遇到此错误并且与 OP 问题并非 100% 相关的人,请检查您是否传递了该值,并且在 spring-boot 的情况下它不为空:@Value 注释。

答案 3 :(得分:0)

也许仅在您的IDE编码设置中存在问题。尝试在各处设置UTF-8:

enter image description here

答案 4 :(得分:0)

在 API 密钥授权场景中...

在从第一次 REST 调用中获得 AUTH_TOKEN 和 ENDPOINT_URL 后,您可能正在执行第二次 REST 调用。

检查“ + ”的串联,您可能只发送 API_METHOD_URI。

这发生在我使用 Streamsets 集成平台尝试连接到 Oracle 的 Responsys API 时。