UriComponentsBuilder in Springboot

UriComponentBuilder class helps to create UriComponents instances by providing control over various aspects of Uri like creating,expansion etc.It is very useful in getting query parameter,url etc.

The dependency needed for UriComponentBuilder is:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.2.2.RELEASE</version>
</dependency>

Below are some of the key operation in the code:

import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

public class sample_uri {

    public static void main(String[] args) {

        String uri = "https://www.thebadengineer.com:9082/api/post/tags/search?name=springboot&count=4";
        UriComponents uriComponentsBuilder = null;

        uriComponentsBuilder = UriComponentsBuilder.fromUriString(uri).build();

        //various opeartion can be done now on this object
        System.out.println(uriComponentsBuilder.getHost());
        //Output: www.thebadengineer.com

        System.out.println(uriComponentsBuilder.getQueryParams());
        //Output: {{name=[springboot], count=[4]}

        System.out.println(uriComponentsBuilder.getQuery());
        //Output: name=springboot&count=4

        System.out.println(uriComponentsBuilder.getPath());
        //Output: /api/post/tags/search

        System.out.println(uriComponentsBuilder.toString());
        //Output: https://www.thebadengineer.com:9082/api/post/tags/search?name=springboot&count=4

        System.out.println(uriComponentsBuilder.toUri());
        //Output: https://www.thebadengineer.com:9082/api/post/tags/search?name=springboot&count=4

        System.out.println(uriComponentsBuilder.getPort());
        //Output: 9082

        System.out.println(uriComponentsBuilder.getScheme());
        //Output: https

        System.out.println(uriComponentsBuilder.getUserInfo());
        //Output: null

    }
}

Also to get query Parameters(which is most commonly used) we can make use of the MultiValueMap as given below:

import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;

public class sample_uri {

    public static void main(String[] args) {

        String uri = "https://www.thebadengineer.com:9082/api/post/tags/search?name=springboot&count=4";
        UriComponents uriComponentsBuilder = null;
        List<String> name=null,count=null;

        uriComponentsBuilder = UriComponentsBuilder.fromUriString(uri).build();

        MultiValueMap<String, String> parameters = uriComponentsBuilder.getQueryParams();

        if(parameters!=null)
        {
            name = parameters.get("name");
            count = parameters.get("count");
        }
        System.out.println("Name is: " + name.get(0));
        System.out.println("Count is: " + count.get(0));
    }
}
/*Output is : Name is: springboot
              Count is: 4*/

Above code was for Springboot.If someone wants to perform all this steps without springboot can take help of Java.net.URI class .This class provides methods for creating URI instances from its components or by parsing the string form of those components. It is used for accessing and retrieving different components of a URI instance.


Follow: https://www.geeksforgeeks.org/java-net-uri-class-java/