我的application.properties中有以下兩個REST URL。如何根據maven配置文件命中特定的REST URL?
我想獲取一個但不是基於動態參數,但不知道如何。我嘗試使用maven配置文件,但不知道如何閱讀Java代碼中的maven配置文件並獲取基於此的url。
請指導。
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>
你可以有你'application.properties'文件的多個版本編譯的類。根據Maven配置文件,在您的應用程序中打包正確的配置文件。 –
你能指點我一些文檔或例子嗎? – user2325154
爲什麼你甚至需要這樣做?將配置文件放在包含此URL的JAR之外,然後代碼將簡單地讀取該文件中的任何內容。然後您可以隨時編輯該文件,並重新啓動您的應用程序,不需要涉及任何奇怪的定製打包/配置文件。 – Tunaki