Replace only part of the URL host with java

Replace only the host and port numbers that are part of the URL in java. Here's how to use spring's UriComponentsBuilder.

Source code

Almost a copy of https://stackoverflow.com/questions/46667342/java-replace-host-in-url. Specify the original URL in UriComponentsBuilder and then replace it with a method such as `` `host```.

import org.springframework.web.util.UriComponentsBuilder;

        String url = "http://localhost:8080/example?param=hoge";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        String url2 = builder.host("hoge-host").toUriString();

        System.out.println(url2);// http://hoge-host:8080/example?param=hoge

Reference URL

Recommended Posts

Replace only part of the URL host with java
Qualify only part of the text
[Java] Access the signed URL of s3 (signed version 2)
[Java] The confusing part of String and StringBuilder
Calculate the similarity score of strings with JAVA
[Java] How to get the URL of the transition source
Get the URL of the HTTP redirect destination in Java
CI the architecture of Java / Kotlin applications with ArchUnit
Monitor the internal state of Java programs with Kubernetes
Check the behavior of Java Intrinsic Locks with bpftrace
The story of making dto, dao-like with java, sqlite
[Java] Cut out a part of the character string with Matcher and regular expression
[Java] Simplify the implementation of data history management with Reladomo
Be sure to compare the result of Java compareTo with 0
[Java] Delete the elements of List
Java to learn with ramen [Part 1]
Replace the contents of the Jar file
[Java1.8 +] Get the date of the next x day of the week with LocalDate
[Java version] The story of serialization
Try Hello World with the minimum configuration of Heroku Java spring-boot
Basic usage of java Optional Part 1
Connecting to a database with Java (Part 1) Maybe the basic method
Replace with a value according to the match with a Java regular expression
Follow the link with Selenium (Java)
A story about hitting the League Of Legends API with JAVA
Get the public URL of a private Flickr file in Java
The point of addiction when performing basic authentication with Java URLConnection
[Java] Appropriate introduction by the people of Tempa Java Part 0 (Code rules)
Server processing with Java (Introduction part.1)
Overwrite upload of file with the same name with BOX SDK (java)
Is the version of Elasticsearch you are using compatible with Java 11?
The story of making a game launcher with automatic loading function [Java]
Let's express the result of analyzing Java bytecode with a class diagram
Specify the favorite IP of the host network with docker-compose and start it
Check the domain by checking the MX record of the email address with java
[Java] Check if the character string is composed only of blanks (= Blank)
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
Get the result of POST in Java
Check the contents of the Java certificate store
Examine the memory usage of Java elements
[Java] Get the day of the specific day of the week
Memo: [Java] Check the contents of the directory
part of the syntax of ruby ​​on rails
Compare the elements of an array (Java)
Getting Started with Java Starting from 0 Part 1
[day: 5] I summarized the basics of Java
What are the updated features of java 13
Looking back on the basics of Java
Output of the book "Introduction to Java"
About the treatment of BigDecimal (with reflection)
The story of writing Java in Emacs
Format the contents of LocalDate with DateTimeFormatter
[Java] Check the number of occurrences of characters
Try using the Wii remote with Java
[Java] Get the date with the LocalDateTime class
<Java> Quiz to batch convert file names separated by a specific character string with a part of the file name
Increment with the third argument of iterate method of Stream class added from Java9
[Code] Forcibly breaks through the C problem "* 3 or / 2" of [AtCoder Problem-ABC100] with Java [Code]
Validate the identity token of a user authenticated with AWS Cognito in Java
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain