Read Data From YAML File in Spring Boot Framework
Read Data From YAML File in Spring Boot Framework
Listen Share
Topics to be covered:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=user
spring.datasource.password=user
If you see the above configuration in this “spring.datasource” common for all.
This is a sample example, Thinks about a large project.
Below config is an example of YAML. Look, It is more formatted and structured
with no duplicates(“spring.datasource”)
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: user
password: user
You can do with your spring boot application as well. Trust me it is more readable
and easy to use.
myservice-config:
#My application constants/config properties starts from here
external-service-config:
payment_url: "http://localhost:8081/payment
auth_url: "http://localhost:8083/auth
login_url: "http://localhost:9999/login"
swagger-config:
title: "Dummy Service APIs"
description: "Rest API's for dummy Service"
version: "version-1"
redis-config:
host: localhost
port: 6379
rollbar-config:
access_token: "3egfhcd2c0978888hhh9b0bcdc445"
environment: "local"
aws-config:
access_key: MKIKAINKTWT123345898
secret_key: LMdhahdj777/jjijkajdk7
bucket_region: ap-south-1
cloud_folder: uploads/local
#ends here
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: user
password: user
How to read these config property, like aws access key, rollbar access key etc
import lombok.Getter;
import lombok.Setter;
import
org.springframework.boot.context.properties.ConfigurationPropertie
s;
@ConfigurationProperties(prefix = "myservice")
@Getter
@Setter
public class AppProperties {
@Getter
@Setter
public static class ExternalServiceConfig {
private String paymentUrl;
private String authUrl;
private String loginUrl;
}
@Getter
@Setter
public static class SwaggerConfig {
private String title;
private String description;
private String version;
}
@Getter
@Setter
public static class RedisConfig {
private String host;
private Integer port;
}
@Getter
@Setter
public static class RollbarConfig {
private String accessToken;
private String environment;
}
@Getter
@Setter
public static class AwsConfig {
private String accessKey;
private String secretKey;
private String bucketRegion;
private String cloudFolder;
}
}
@SpringBootApplication
@EnableConfigurationProperties(AppProperties.class)
public class YAMLTestDemoApplication {
public static void main(String[] args) {
SpringApplication.run(YAMLTestDemoApplication.class, args);
}
}
annotationProcessor "org.springframework.boot:spring-boot-
configuration-processor"
Now AppProperties contains all myservice-config details and it’s ready to use.
How do we use
Sample example how you can use AppProperties/YAML data in configuration.
@Configuration
@AllArgsConstructor
public class AwsS3Client {
private final AppProperties appProperties;
@Bean
public AmazonS3 getClient() {
final BasicAWSCredentials basicCredentials = new
BasicAWSCredentials(appProperties.getAwsConfig().getAccessKey()
, appProperties.getAwsConfig().getSecretKey());
return AmazonS3ClientBuilder.standard()
.withCredentials(new
AWSStaticCredentialsProvider(basicCredentials))
.withRegion(appProperties.getAwsConfig().getBucketRegion())
.build();
}
}
Note: How you can enable or disable swagger-ui in different profiles.
UseCase: I don’t need swagger-ui in production env so no one can use it.
Add this configuration to your production’s .yml file
springdoc:
api-docs:
enabled: false
Spring Boot Yml Yaml S3 Bucket Swagger Ui
Follow
Written by Jhamukul
77 Followers
Jhamukul
144 4
Jhamukul
44
Jhamukul
4 1
Jhamukul
Shashi Kumar
1
Alexander Obregon
41 1
Lists
52
183 4
Hasan Khan in Stackademic
12