I am trying to get a plain text file from Spring Cloud Config Server using a Spring Boot application. I have put a plain text file example.json in the config git repo in the following structure:
config/appName/schema/example.json
While I am able to access http://localhost:8888/dod-sync/default/master/schema/example.json from the browser, I don't want to just open a HttpConnection and get the file content and parse it in the application.
Is there a way I can load this file somewhat similar to the way to retrieve normal properties in my Spring application?
For example, I can use #Value("${application.http.timeout}") to get the timeout property. I want to have a similar functionality to get the plain text content in the application.
I read https://cloud.spring.io/spring-cloud-config/reference/html/#_serving_plain_text and lots of resources on web but can't find anything helpful.
Thank you for any help!
Is it a property file read case?
try {
InputStream input = new FileInputStream(ConfigJsonTest.class.getResource("/config.properties").getFile());
Properties prop = new Properties();
prop.load(input);
System.out.println(prop.get("timeout"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Related
I want to write a property to application.properties before spring boot application start.
Properties properties = new Properties();
try {
properties.load(new FileInputStream("src/main/resources/application.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (properties.getProperty("name") == null) {
OutputStream os = null;
Properties prop = new Properties();
prop.setProperty("name", "WriteTheName");
try {
os = new FileOutputStream("src/main/resources/application.properties", true);
prop.store(os, "Dynamic Property File");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
else
{
System.out.println("Already available Name %%%%%%%%%%%%%%%%%%%%%%%");
}
The above code, i used with CommandLineRunner's run method. This is writing the value after application started.
Please let me know if there is a way to write the property value before application start up.
When the app is built and started, you won't have access to src/main/resources.
Writing a properties file after starting an app but before starting to load the Spring context is not a good approach. One of the few use cases I see with this is during an application installation where maybe some random ids would have to be generated but this is the job of the installer and not the job of the application.
Alternatively you can pass property values to a Spring Boot application as:
Assuming the property you would like to use is named: my.property.key:
java -Dmy.property.key=value -jar application.jar
java -jar application.jar --my.property.key=value
MY_PROPERTY_KEY=value java -jar application.jar
First of all i've tried my code by extracting the war file (using maven) on both eclipse and on a tomcat 8.0.33 stand alone on my mac.
I have also tried my code on a windows server 2008 with the same java version 1.8, and it works when i put some variable (which are username and password) hardcoded, but when i make the code to read them from a reousrce file, it is just working on my mac (both eclipse and tomcat stand alone), but not on the server
this is the code to read the resource
private static String getUsername() {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(MyConfiguration.class.getClassLoader()
.getResource("configuration.properties").getFile());
// load a properties file
prop.load(input);
// get the property value and print it out
return prop.getProperty("username");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
where the location of the configuration.properties is in the src/main/resources and on the server, i can see that file is in the correct directory.
i am using maven, i don't know what other information you need to help me, but if you say, i will give you
You may try
input = Thread.currentThread().getContextClassLoader().getResourceAsStream("configuration.properties");
Open your war and the jar file that contains your application. Ensure that the resource you are trying to open is in the jar (in the war) that is getting deployed. Most of the times this has happened to me, it's because I failed to tell my build process that this file needed to be copied over to the deployment. Class files go automatically, but not always resource files.
I am currently writing a spring boot program that should populate an XLSX template with data and am trying to read the template from inside the project. My directory structure is as follows:
I am now trying to access the file using the following code:
try {
InputStream fis = this.getClass().getResourceAsStream("xlstemplates/billingReview.xlsx");
XSSFWorkbook billingReviewTemplate = new XSSFWorkbook(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Unfortunately I have yet to find a way to read the xlsx file into an InputStream. It always comes back null. Anyone have any pointers on how I can reference that file that is included in the project?
I am new to play framework. I want to know how to use the property file in play framework.
My property file is,
conf/test.properties
name=kumar
framework=playframework
so now i want to use test.properties inside my controller class (Application.java).
Please let me know what are the steps i need to do.
All files placed in the conf/ folder are automatically added to your classpath. You should be able to access the your file like this:
Properties prop;
try {
prop = new Properties();
InputStream is = this.getClass().getResourceAsStream("test.properties");
prop.load(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String name = prop.getProperty("name");
String framework = prop.getProperty("playframework");
Note: I haven't tested any of the above.
Update:
I've just realized that this question is a close duplicate of Load file from '/conf' directory on Cloudbees but since my solution also includes how to access the properties in the file, I'll leave it as is.
Also since your controller method will most likely be static the above might fail to compile. In the answer I referenced they suggested using the facility provided by Play!:
Play.application().resourceAsStream("test.properties")
you can access to property files in play framework by doing:
val ConfigLoader = play.Play.application.configuration
implicit val connector = ContactPoints(Seq(ConfigLoader.getString("cassandra.host"))).keySpace(ConfigLoader.getString("cassandra.keyspace"))
In build a web application with netbeans, in some point I extract an arrayList to json format.
public void convertToJSON(ArrayList list){
ObjectMapper mapper = new ObjectMapper();
try{
mapper.writeValue(new File("C:\\temp\\data.json"), list);
}catch(JsonGenerationException e){
e.printStackTrace();
}catch(JsonMappingException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
and this creates the file C:\temp\data.json. How can I create this file inside my project folder where JSPS's exist? I tried determine the location with
String dir = System.getProperty("user.dir");
but this created the file in C:\Program Files\Apache Software Foundation\Apache Tomcat 8.0.15\bin. I want to create the file inside the projects folder in order to use it to show my data in bootstrap table. There I define the location of my data by
data-url="data.json"
I tried this data-url="C:\temp\data.json" but i got an error
XMLHttpRequest cannot load file:///C://temp//data.json?order=desc&limit=10&offset=0. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
This is cross domain problem, bootstrap table does not support file:// url, please use an web server to run your code. Here is a issue can help you: https://github.com/wenzhixin/bootstrap-table/issues/230
To get a File inside the project's folder you can use
URL resourceUrl = this.getClass().getResource("");
File file = new File(resourceUrl.toURI().toString() + "data.json");