Using System path in Spring Boot application.properties - spring

In the actual production and development servers, the base path is set with the code below.
private static String FILE_DIR = System.getProperty("user.dir") + File.separator + "FILES";
I want to move this code to application.properties.
Is there a way to use a path like System.getProperty in application.properties?
Sorry for such a novice question

I tried the following in application.yml and it worked.
filedir: ${user.dir}${file.separator}FILES
And then
#Value("${filedir}")
private String fileDir;
Please let me know it it wrks for you.

Related

Spring Boot: Can't find File located in resources folder with #Value annotation

Simple problem (I think): I'm attempting to load files with the #Value annotation, but Spring Boot does not see a file there.
#Value("classpath:/*.xlsx")
private Resource[] inputResources;
I'm using inputResources as an input to a Spring Batch method, which then fetches all files matching my wildcard.
MultiResourceItemReader<Employee> resourceItemReader = new MultiResourceItemReader<Employee>();
resourceItemReader.setResources(inputResources);
The Excel spreadsheets are located in my resources folder. Any reason why this wouldn't work??
#Value Annotation is used in Spring to read a single value from a property file. It can not be used to read a whole file.
To read a file try something like this:
Resource resource = new ClassPathResource("classpath:data.txt");
InputStream inputStream = resource.getInputStream();
...
You could use #Value to have this String "classpath:data.txt" not hardcoded.

How can I inject OS hostname value in Spring Boot?

Is it possible to do something like that?
#Value("${xxx.hostname}")
private String hostname;
Is it also possible to get any other System Environment Property like that?
Spring Boot automatically allow you to take environment variable.
You can just do #Value("${HOSTNAME}") (if HOSTNAME is defined ofc)
You can also use your application.properties to have something like :
xxx.hostname = ${HOSTNAME}

spring boot app cannot load bundle properties files

I am building an app that mostly provide REST services, nothing fancy. since my data consumed by the app can have multiple languages I thought about using the bundle files.
I created 3 files, one with the default file name and another two with specific languages. The files created using intellij IDE I am using.
I followed this guide https://www.baeldung.com/java-resourcebundle however on each run I am getting:
MissingResourceException: Can't find bundle for base name tp_app_strings, locale en_US
I tried numerous articles but none of them seems to resolve the issue.
One fun fact is that if I am using the #Value("classpath:tp_app_strings.properties") on a 'Resource' field I am able to get a reference to that file, so it spring is able to find it.
Additional thing that I tried was to create a WEB-INF directory and place the files there (read it in some article) but still no positive affect
The project structure is quite straight forward:
Spring boot version 2.2 running tomcat.
Any suggeestions would be highly appriciated
You can load the .properties file to the application context using #PropertySource annotation instead using #Value to load the .properties file to a org.springframework.core.io.Resource instance.
The usage;
#Configuration
#PropertySource("classpath:tp_app_strings.properties")
public class DefaultProperties {
#Value("${property1.name}") // Access properties in the above file here using SpringEL.
private String prop1;
#Value("${property2.name}")
private String prop2;
}
You wouldn't need java.util.ResourceBundle access properties this way. Use different or same class to load other .properties files as well.
Update 1:
In order to have the functionality of java.util.ResourceBundle, you can't just use org.springframework.core.io.Resource class. This class or non of it sub-classes don't provide functions to access properties by its name java.util.ResourceBundle whatsoever.
However, if you want a functionality like java.util.ResourceBundle, you could implement something custom like this using org.springframework.core.io.Resource;
#Configuration
public class PropertyConfig {
#Value("classpath:tp_app_strings.properties")
private Resource defaultProperties;
#Bean("default-lang")
public java.util.Properties getDefaultProperties() throws IOException {
Properties props = new Properties();
props.load(defaultProperties.getInputStream());
return props;
}
}
Make sure to follow correct naming convention when define the property file as java.util.Properties#load(InputStream) expect that.
Now you can #Autowire and use this java.util.Properties bean wherever you want just like with java.util.ResourceBundle using java.util.Properties#getProperty(String) or its overloaded counterpart.
I think it's problem of you properties file naming convention. use underline "_" for specifying locale of file like
filename_[languageCode]_[regionCode]
[languageCode] and [regionCode] are two letters standard code that [regionCode] section is optional
about code abbrivation standard take a look on this question
in your case change file name to tp_app_strings_en_US.properties

spring-boot-starter-data-mongodb don't include GridFSBucket bin as autoconfigured

GridFSBucket is not auto configured in Spring boot parent in 2.1.3.REALEASE, so cant be autowired. But GridFsTemplate is autowire as it is autoconfigured. how to get gridFSBucket from gridFsTemplate. Actually i want to use 'downloadToStream' method to write to a file. If that is possible by GridFSFile then not required.
I had the same issue while migrating, trying to load a PDF stored in MongoDB, causing the file to be corrupted (startxref not found)
Following this answer helped me: https://stackoverflow.com/a/50732308/590374
GridFSFile file = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(fileId)));
GridFsResource resource = gridFsTemplate.getResource(file);
return resource.getInputStream();

Spring Rest URL /rest/v1 is beeing added

I am working on a existing project where the restControllers has a restmapping #RequestMapping(value = "/test"). There is no base URL added. I have checked all the option where the baseURL gets added but i dont find any in my application. When i run my server. The way to access is <hostname>/rest/v1/test.Application also uses Spring Hateoas. Can you let me know from where/how do these additional /rest/v1 is getting added?
You should find it configured in web.xml or some spring config xml where the servlet mapping url is done.
If the project is generated using maven then you should see the application name in pom.xml file something like this.
<groupId>com.test</groupId>
<artifactId>rest</artifactId>
<packaging>war</packaging>
<version>0.1.0.BUILD-SNAPSHOT</version>
<name>rest</name>
and also check web.xml file inside webappp/WEB-INF directory or any other XML config file if you have.
Probably you have one of these property set in the application.properties file:
server.contextPath=/rest/v1
or
spring.data.rest.basePath=/rest/v1
You can control the base path of you application from there.
Hope it helps!
someone configure it somewhere. search for it in:
application.properties
server.servlet-path = or spring.data.rest.basePath =
Configuration file: just like #Arnad said
Configuration Classes: search for a bean named RepositoryRestConfigurer
search in whole project:
in eclipse use Ctrl + h -> File search
in intellij: ctrl + shift + f
ref link : http://docs.spring.io/spring-data/rest/docs/current/reference/html/#_changing_the_base_uri
With very minimal code that you have added, I can only tell you possible place where the base url might have been configured.
On top of your #RestController annotated class.
In case of WebApplicationInitializer it must have been added on ServletRegistration.Dynamic something like below:
ServletRegistration.Dynamic dispatcher = container
.addServlet("dispatcher", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/rest/v1/");

Resources