Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/java lang illegalargumentexception uri is not absolute. How to fix?
Next
Answered
Skyla Wilderman
  • 3
Skyla Wilderman
Asked: July 11, 20222022-07-11T04:48:11+00:00 2022-07-11T04:48:11+00:00In: java

java lang illegalargumentexception uri is not absolute. How to fix?

  • 3

. Advertisement .

..3..

. Advertisement .

..4..

I meeet the exception java lang illegalargumentexception uri is not absolute as seen in the spring boot below.

2020-10-06 17:15:08.417 ERROR 20534 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: URI is not absolute] with root cause

java.lang.IllegalArgumentException: URI is not absolute
at java.net.URI.toURL(URI.java:1088) ~[na:1.8.0_101]
at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest(SimpleClientHttpRequestFactory.java:145) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:124) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]

Please help me find out the cause and solution for it. Thanks.

  • 1 1 Answer
  • 330 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-07-11T05:17:28+00:00Added an answer on July 11, 2022 at 5:17 am

    The cause: When a relative url is used to specify the resource, java lang illegalargumentexception uri is not absolute is thrown. The whole url will be utilized to make the rest call in the spring boot RestTemplate.

    Solution:

    Solution 1: The relative url must be converted to an absolute url if it is let into the rest call in the RestTemplate. The absolute url will be used by the RestTemplate to locate the resource. The whole url “http:/localhost:8080/student” is being used to execute a rest call with RestTemplate in the example below.

    package com.yawintutor;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    public class TestController {
    
    @GetMapping(value = "/student")
    public Student getStudent() {
    return new Student(1, "name1");
    }
    
    @GetMapping(value = "/getstudent")
    private Student getStudentObject(HttpServletRequest request)
    {
    String uri = "http://localhost:8080/student";
    RestTemplate restTemplate = new RestTemplate();
    Student result = restTemplate.getForObject(uri, Student.class);
    return result;
    }
    }

    Solution 2:

    Use the HTTPRequest object to create a complete url if it is using a relative url inside a rest call that points to the same Tomcat server before passing it to the rest template. The example that follows shows how to build an entire url from a relative url to the same server.

    package com.yawintutor;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    public class TestController {
    
    @GetMapping(value = "/student")
    public Student getStudent() {
    return new Student(1, "name1");
    }
    
    @GetMapping(value = "/getstudent")
    private Student getStudentObject(HttpServletRequest request)
    {
    String requestURL = request.getRequestURL().substring(0, request.getRequestURL().indexOf(request.getRequestURI()));
    String uri = requestURL + "/student";
    RestTemplate restTemplate = new RestTemplate();
    Student result = restTemplate.getForObject(uri, Student.class);
    return result;
    }
    }

    Solution 3:

    When using a relative url in a RestTemplate and needing to refer to an external host server, its url can be externalized inside the application.properties files. The relative url can have the properties added, like in the example below.

    application.properties

    external.server.url=http://www.yawintutor.com

    TestController.java

    package com.yawintutor;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    public class TestController {
    
    @Value("${external.server.url}")
    String externalServerURL;
    
    @GetMapping(value = "/student")
    public Student getStudent() {
    return new Student(1, "name1");
    }
    
    @GetMapping(value = "/getstudent")
    private Student getStudentObject()
    {
    String uri = externalServerURL + "/student";
    RestTemplate restTemplate = new RestTemplate();
    Student result = restTemplate.getForObject(uri, Student.class);
    return result;
    }
    }
    • 1
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.