I have the following two REST Urls in my application.properties.
I would like to fetch one but not both based on dynamic parameter but not sure how. I tried using maven profile but not sure how to read the maven profile in Java code and get the url's based on that.
Please guide.
application.properties
rest.uri=http://localhost:8080/hello
mock.rest.uri=http://localhost:9999/hello
RestClient.java
public class HelloWorldClient {
public static void main(String[] args) {
try {
Client client = Client.create();
//getRestUrl() METHOD CALL NEEDS TO BE DYNAMIC
//EITHER MOCK URL OR ACTUAL REST URL SHOULD BE FETCHED HERE
// NOT SURE HOW ???????
WebResource webResource = client.resource(getRestUrl());
ClientResponse response = webResource.accept("text/plain").get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("\nOutput from Server.... " + output);
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getRestUrl() throws IOException {
Properties prop = GenericUtils.loadProperties("application.properties");
String restUri = prop.getProperty("rest.uri");
String mockRestUri = prop.getProperty("mock.rest.uri");
System.out.println("restUri = " + restUri);
System.out.println("mockRestUri = " + mockRestUri);
return mockRestUri;
}
}
pom.xml
<profiles>
<profile>
<id>rest-server</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>mock-rest-server</id>
</profile>
</profiles>
Based on the solution provided by Jose and Fetta above I altered the program and hereby aggregating both the solutions into this post and posting it here.
application.properties
rest.uri=${rest.uri}
pom.xml
<build>
<filters>
<filter>src/main/resources/application.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>rest-server</id>
<properties>
<rest.uri>http://localhost:8080/hello</rest.uri>
</properties>
</profile>
<profile>
<id>mock-rest-server</id>
<properties>
<rest.uri>http://localhost:9999/hello</rest.uri>
</properties>
</profile>
</profiles>
HelloWorldClient.java
public class HelloWorldClient {
public static void main(String[] args) {
try {
Client client = Client.create();
WebResource webResource = client.resource(getRestUrl());
ClientResponse response = webResource.accept("text/plain").get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("\nOutput from Server.... " + output);
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getRestUrl() throws IOException {
Properties prop = GenericUtils.loadProperties("application.properties");
String restUri = prop.getProperty("rest.uri");
System.out.println("restUri = " + restUri);
return restUri;
}
}
Compile the class using profiles
mvn clean install -Prest-server
mvn clean install -Pmock-rest-server
Run the main method
mvn exec:java -Dexec.mainClass="com.example.HelloWorldClient"