Spring Boot - How to define application.yml properties as application.properties - java

I am currently trying to set up an s3 bucket with my Spring Boot webapp for adding/removing images.
The guide I am following uses the following application.yml properties:
amazonProperties:
endpointUrl: https://s3.us-east-2.amazonaws.com
accessKey: XXXXXXXXXXXXXXXXX
secretKey: XXXXXXXXXXXXXXXXXXXXXXXXXX
bucketName: your-bucket-name
How can I define these properties in my application.properties file?
All help is very much appreciated, thank you!

Try to just split them in different lines in your application.properties:
amazonProperties.endpointUrl= https://s3.us-east-2.amazonaws.com
amazonProperties.accessKey= XXXXXXXXXXXXXXXXX
amazonProperties.secretKey= XXXXXXXXXXXXXXXXXXXXXXXXXX
amazonProperties.bucketName= your-bucket-name

if you're using spring Spring Cloud AWS maven dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws</artifactId>
</dependency>
use the following properties in your application.properties file.
cloud.aws.region.static=your region
cloud.aws.credentials.accessKey=your access key
cloud.aws.credentials.secretKey=your secret key
app.awsServices.bucketName=bucketName
refer: https://cloud.spring.io/spring-cloud-static/spring-cloud-aws/1.2.3.RELEASE/multi/multi__basic_setup.html

Related

Conditionnaly replace spring boot config yml property

I would like to replace a configuration yml property using a condition based on environnement variables :
spring:
datasource:
username:${ENV} == 'PROD' ? ${USER_PROD} : ${USER_TEST}
password: ${ENV} == 'PROD' ? ${PWD_PROD} : ${PWD_PROD}
Is there any way I can do this inside my application.yml or programmaticaly ?
I have not faced this situation before
The normal way of doing this is to use different application.properties file each representing a "profile".
Then you can override the desired properties based on the profile and run the application using that profile using -Dspring.profiles.active.
A useful guide on the following link.
you can use separate config files and properties for different Spring Profiles.
please read this guide for more info: Guide

Spring Cloud Configuration - Access shared properties file in all microservices

Inside my spring cloud config server application.properties. I have passed #EnableConfigServer in my application class
spring.application.name=CONFIG_SERVER
server.port=1080
spring.cloud.config.server.git.uri=PATH_TO_GITHUB_REPO
spring.cloud.config.server.git.username=USNM
spring.cloud.config.server.git.password=PWD
spring.cloud.config.server.git.skip-ssl-validation=true
Inside my git repo application.properties
third-party-cred=MY_VALUE
In my spring cloud config client bootstrap.properties
server.port=1081
spring.application.name=MY_SERVICE
spring.cloud.config.uri=http://localhost:1080
I am trying to access property present in git repo using #Value inside my microservice but it is giving error Could not resolve placeholder 'third-party-cred' in value "${third-party-cred}"
Solution
bootstrap.properties is not enable by default. Please add this dependency inside your microservice.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

Spring Boot - Initialization Order of Dependencies

I have a Spring Boot Project which uses Jasypt for encrypting properties in the application.yml file.
Jasypt has always initialized and decrypted the passwords, before any dependecy which uses the decrypted password, asked for it.
However I now want to use Azure Key Vault aswell. This dependency now tries to access its properties before they have been decrypted by Jasypt.
How can I change the order in which Spring Boot initializes these dependencies?
For both dependencies I do not have defined any #Bean in the application.
I have created a Demo-Repository.
Its a plain Spring Boot Project. Only thing which is changed is the following:
Added both dependencies in pom.xml:
<!-- Azure Key Vault -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-keyvault-secrets-spring-boot-starter</artifactId>
<version>2.2.5</version>
</dependency>
<!-- Jasypt -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
And in application.yml:
jasypt:
encryptor:
password: test
azure:
keyvault:
enabled: true
uri: ENC(1pGS+OSU9a9Bs+2iAjhyVd8NonXkLp0BsPBOuUzcyJSFnABs+bc5jw==)
client-id: test
client-key: test
tenant-id: test
The Error:
java.lang.IllegalArgumentException: The Azure Key Vault url is malformed.
(...)
Caused by: java.net.MalformedURLException: no protocol: ENC(1pGS+OSU9a9Bs+2iAjhyVd8NonXkLp0BsPBOuUzcyJSFnABs+bc5jw==)
As you can see, first Azure Key Vault Initializes itself, tries to use the azure.keyvault.uri but Jasypt hasn't decrypted it yet.
What I would expect in this case is that it tries to connect but isn't able to connect because the URL doesn't exist. But it atleast should use the decrypted version.
I am greatful for any suggestions.
I actually found the solution with the help of the Jasypt docs on GitHub (Section: Custom Environment)
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
//Doesn't work
//SpringApplication.run(DemoApplication.class, args);
//This works:
new SpringApplicationBuilder().environment(new StandardEncryptableEnvironment())
.sources(DemoApplication.class).run(args);
}
}
(...) While not required in most scenarios could be useful when customizing Spring Boot's init behavior or integrating with certain capabilities that are configured very early, such as Logging configuration. (...)
The important part is that you specify the StandardEncryptableEnvironment.
This will make it that the very first thing Spring (Jasypt) does is decrypt the Variables. Everything else stays in the same order.

Spring boot inject mongodb datasource

I am trying to deploy a spring boot application connected to a mongodb instance to cloud foundry.
cf create-service MongoService default my-mongo
cf push myapp --no-start
cf bind-service myapp my-mongo
cf start myapp
The connection details to the mongodb instance are in the VCAP_SERVICES environment variable. When deploying my application to cloudfoundry spring boot is trying to access mongodb on localhost:27017 and obviously fails.
I would like to parse the VCAP_SERVICES environment variable, construct some mongodb connection details from it and provide this as a spring bean. Which class should I use for these configuration details?
With Spring Boot, you don't need to manually parse VCAP_SERVICES. If you are using MongoTemplate or MongoRepository, it will automatically connect to the bound instance.
Make sure that you have spring-boot-starter-parent identified as the parent artifact in your pom.xml.
You can add the following to your pom.xml to ensure that the cloud connector code is getting picked up:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cloud-connectors</artifactId>
</dependency>
Of course, you also need the MongoDB Spring Data dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
You may want to try setting up the configuration parameters, like a workaround I had to implement with MySQL, check this question.
For me it all came down to properly defining following properties:
spring:
datasource:
url: jdbc:mysql://${vcap.services.mydb.credentials.host}:${vcap.services.mydb.credentials.port}/${vcap.services.mydb.credentials.name}
driver-class-name: com.mysql.jdbc.Driver
username: ${vcap.services.mydb.credentials.user}
password: ${vcap.services.mydb.credentials.password}

How to replace properties files by yml files in Spring boot

Is there anything special to say to Springboot to take in account as configuration yaml files instead the properties files? I'm using Maven and Springboot in a project, if I place the file application.properties in the resources file the configuration is automatically recognised by Springboot.
However just replacing the properties file by a yml file does not work, the file is not taking in account anymore.
Anything to add to splicitely say to SpringBoot to use the yml files?
Below both files:
application.properties:
# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=/WEB-INF/views/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
and application.yml
spring:
profiles:
active: ${dario.environment:dev}
thymeleaf:
check-template-location: true
thymeleaf.prefix: /WEB-INF/views/
thymeleaf.suffix: .html
thymeleaf.mode: LEGACYHTML5
spring.thymeleaf.encoding: UTF-8
spring.thymeleaf.content-type: text/html
cache: false
If you refer to the section 'Using YAML instead of Properties' in the Spring Boot reference documentation, it says that Spring Boot should pick it up automatically given you have the SnakeYAML library on your classpath.
Having that said, it also states
If you use ‘starter POMs’ SnakeYAML will be automatically provided via
spring-boot-starter

Categories