Analysis and Resolution of URI Not Absolute Exception in Java RESTful Web Service Calls

Nov 26, 2025 · Programming · 10 views · 7.8

Keywords: Java | RESTful Web Services | URI Exception

Abstract: This article provides an in-depth analysis of the URI not absolute exception encountered when calling RESTful web services using the Jersey client in Java. By examining the root cause of the exception, it explains the distinction between absolute and relative URIs and offers corrected code implementations. Through detailed code examples, the article demonstrates how to avoid URI encoding issues caused by misuse of URLEncoder, ensuring stable and correct web service invocations.

Problem Background and Exception Analysis

When invoking RESTful web services in Java applications, developers often utilize the Jersey client library for HTTP request handling. However, improper URI processing can lead to the java.lang.IllegalArgumentException: URI is not absolute exception. The fundamental cause of this exception is that the URI passed to the client does not conform to the specifications of an absolute URI.

Conceptual Distinction Between Absolute and Relative URIs

According to Java official documentation, an absolute URI must include a explicit scheme (such as https:// or http://), whereas a relative URI lacks this critical component. In the problematic code, the developer performed URLEncoder.encode(uri) on the complete absolute URI string, which effectively disrupts the integrity of the URI.

Code Error Analysis and Correction

The key error in the original code is: WebResource resource = client.resource(URLEncoder.encode(uri));

The URLEncoder.encode() method is designed to encode the query parameter portion of a URI, not the entire URI. When the full URI https://127.0.0.1:8443/cas-server-webapp-3.5.0/login is encoded, characters like colons and slashes are converted into percent-encoded forms, causing the URI to lose its absolute characteristics.

The correct implementation should be:

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

Encoding Practices and Best Solutions

In practical development, only the query parameter values should be encoded, not the entire URI. Additionally, specifying the correct character encoding (typically UTF-8) is essential to avoid garbled text issues due to encoding mismatches. Referencing other similar exception cases, such as UnknownHostException, reminds developers to ensure service address reachability at the network level.

Conclusion and Recommendations

Proper handling of URI encoding is fundamental to web service calls. Developers should thoroughly understand the components of an absolute URI and avoid unnecessary encoding of the complete URI. By adopting correct parameter encoding methods and comprehensive exception handling mechanisms, the robustness and reliability of applications can be significantly enhanced.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.