Spring application.yaml and boolean values through env file - spring

In my Spring Boot project, I have to pass to application.yaml file the SHOW_SQL boolean value from an environment file.
Here is application.yaml
spring:
...
show-sql: ${SHOW_SQL}
...
server:
port: 8080
Here is my env file (used to start the spring app)
SHOW_SQL=true
During the mvn test process I receive this error
Failed to bind properties under 'spring.jpa.show-sql' to boolean:
Property: spring.jpa.show-sql
Value: "${SHOW_SQL}"
Origin: class path resource [application.yaml] - 13:15
Reason: failed to convert java.lang.String to boolean (caused by java.lang.IllegalArgumentException: Invalid boolean value '${SHOW_SQL}')
Action:
Update your application's configuration
Is there a way to use boolean values from an .env file and pass them to the application yaml?

Related

Spring boot app access to Azure Keyvault secret

I am trying to set up a Spring boot app to connect to Azure keyvault and fetch secrets.
I have looked across various code samples, but cannot get one to work.
There are no errors related to the keyvault library itself, but the Spring bean does not get initialized with the property fetched from the secret.
I am using Spring boot 2.7.3 and the keyvault starter lib is 4.2.0. This is what I referred to get my property config.
https://microsoft.github.io/spring-cloud-azure/4.2.0/reference/html/appendix.html#_azure_key_vault_secrets_properties
application.yml
------------
spring:
cloud:
azure:
keyvault:
secret:
enabled: true
endpoint: https://uri/
credential:
client-certificate-path: <path to pem file>
client-id: <client-id>
profile:
tenant-id: <tenant-id>
build.gradle
--------------
implementation 'com.azure.spring:spring-cloud-azure-starter-keyvault-secrets:4.2.0'
Spring bean
-------------
#Configuration
#Getter
#Setter
public class AKVProperties {
#Value("${MYAPP-DB-LOGIN-PASSWORD}")
private String dbPassword1;
...
Exception at startup:
----------------------
Error creating bean with name 'AKVProperties': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'MYAPP-DB-LOGIN-PASSWORD' in value "${MYAPP-DB-LOGIN-PASSWORD}"
I think the issue is with application.yaml file. you have to set your config settings under property-sources: .
This is the application.yaml I used where everython is set under the property-sources:
spring:
cloud:
azure:
keyvault:
secret:
property-sources:
- credential:
client-id: <CLIENT ID>
client-secret: <CLIENT SECRET>
endpoint: '<END POINT>'
profile:
tenant-id: <TENANT ID>
Here I have retriving the secret like this
#Value("${secret}")
private String secret;
and then I am just console logging them .
Output:
Keyvault:

Jooq and Spring boot: Upgraded JOOQ via starter: failed to bind spring.jooq.sql-dialect' to org.jooq.SQLDialect

So i upgraded spring-boot-parent-starter to 2.2.8.RELEASE, which results in jooq 3.12.4 . Previously i had 3.11.5.
I am getting the following error
Failed to bind properties under 'spring.jooq.sql-dialect' to org.jooq.SQLDialect:
Property: spring.jooq.sqldialect
Value: MYSQL_5_7
Origin: "spring.jooq.SQLDialect" from property source "applicationConfig: [classpath:/config/application.yaml]"
Reason: failed to convert java.lang.String to org.jooq.SQLDialect
Here is what my application.yaml was before
spring:
jooq:
sql-dialect: mysql_5_7
If you read the whole error message you will see this:
Failed to bind properties under 'spring.jooq.sql-dialect' to org.jooq.SQLDialect:
Property: spring.jooq.sql-dialect
Value: MYSQL_5_7
Origin: class path resource [application.properties]:2:25
Reason: failed to convert java.lang.String to org.jooq.SQLDialect
Action:
Update your application's configuration. The following values are valid:
CUBRID
DEFAULT
DERBY
FIREBIRD
H2
HSQLDB
MARIADB
MYSQL
POSTGRES
SQL99
SQLITE
MYSQL_5_7 is not a value supported by jOOQ open source. It's only availble in the pro version
MYSQL_5_7
#Pro
public static final SQLDialect MYSQL_5_7
The MySQL 5.7 dialect.
This dialect is available in commercial jOOQ distributions, only.

Configuring application using application.yaml instead of application.properties

I have a small Quarkus 1.1.0.Final Web application (using Java 1.8). I'm trying to use a YAML file to configure the application (instead of the usual application.properties) but there is no way the application comes up. I'm always getting this not-so-useful error message(s):
13:53:32,494 ERROR [io.qua.dev.DevModeMain] Failed to start Quarkus: java.lang.RuntimeException: io.quarkus.builder.ChainBuildException: No producers for required item class io.quarkus.deployment.builditem.BuildTimeRunTimeFixedConfigurationBuildItem
at io.quarkus.runner.RuntimeRunner.run(RuntimeRunner.java:180)
at io.quarkus.dev.DevModeMain.doStart(DevModeMain.java:177)
at io.quarkus.dev.DevModeMain.start(DevModeMain.java:95)
at io.quarkus.dev.DevModeMain.main(DevModeMain.java:66)
Caused by: io.quarkus.builder.ChainBuildException: No producers for required item class io.quarkus.deployment.builditem.BuildTimeRunTimeFixedConfigurationBuildItem
at io.quarkus.builder.BuildChainBuilder.build(BuildChainBuilder.java:240)
at io.quarkus.deployment.QuarkusAugmentor.run(QuarkusAugmentor.java:112)
at io.quarkus.runner.RuntimeRunner.run(RuntimeRunner.java:113)
... 3 more
13:53:32,519 INFO [io.qua.dev.DevModeMain] Attempting to start hot replacement endpoint to recover from previous Quarkus startup failure
13:53:32,532 ERROR [io.qua.dev.DevModeMain] Failed to start quarkus: java.lang.IllegalArgumentException: workerPoolSize must be > 0
at io.vertx.core.VertxOptions.setWorkerPoolSize(VertxOptions.java:275)
at io.quarkus.vertx.core.runtime.VertxCoreRecorder.convertToVertxOptions(VertxCoreRecorder.java:151)
at io.quarkus.vertx.core.runtime.VertxCoreRecorder.initializeWeb(VertxCoreRecorder.java:104)
at io.quarkus.vertx.http.runtime.VertxHttpRecorder.startServerAfterFailedStart(VertxHttpRecorder.java:115)
at io.quarkus.vertx.http.deployment.devmode.VertxHotReplacementSetup.handleFailedInitialStart(VertxHotReplacementSetup.java:30)
at io.quarkus.dev.RuntimeUpdatesProcessor.startupFailed(RuntimeUpdatesProcessor.java:449)
at io.quarkus.dev.DevModeMain.doStart(DevModeMain.java:191)
at io.quarkus.dev.DevModeMain.start(DevModeMain.java:95)
at io.quarkus.dev.DevModeMain.main(DevModeMain.java:66)
This is my YAML file:
#
# https://quarkus.io/guides/all-config
# https://quarkus.io/guides/config#overriding-properties-at-runtime
quarkus:
datasource:
driver: org.postgresql.Driver
flyway:
migrate-at-start: true
health:
extensions:
enabled: true
hibernate-orm:
dialect: org.hibernate.dialect.PostgreSQL10Dialect
http:
port: 8080
log: # ALL > FINEST > FINER > FINE > CONFIG > INFO > WARNING > SEVERE > OFF
console:
async: true
color: true
enable: true
format: "%d{yyyy-MM-dd HH:mm:ss,SSS} |- %-5p in %c:%L{3.} [%t] - %s%e%n"
level: WARNING
resteasy:
path: /api
smallrye-openapi:
path: /open-api
swagger-ui:
always-include: true
path: /swagger-ui
"%dev":
quarkus:
datasource:
password: postgres
url: jdbc:postgresql://localhost:5432/quarkus_web
username: postgres
flyway:
clean-at-start: true
hibernate-orm:
log:
sql: true
statistics: true
log:
category:
"io.quarkus.arc.processor":
level: OFF
"io.quarkus":
level: INFO
"org.acme":
level: CONFIG
"%prod":
quarkus:
datasource:
password: postgres
url: jdbc:postgresql://localhost:5432/quarkus_web
username: postgres
flyway:
clean-at-start: false
hibernate-orm:
database:
generation: none
sql-load-script: no-file
"%test":
quarkus:
datasource:
password: postgres
url: jdbc:postgresql://localhost:5432/quarkus_web
username: postgres
flyway:
clean-at-start: true
log:
category:
"io.quarkus":
level: WARNING
"org.acme":
level: WARNING
Anyone using this approach already and succeeded? I'm including io.quarkus:quarkus-config-yaml:1.1.0.Final.
One thing that I've noticed is that I can't separate the profiles using --- as I do with Spring Boot. I think I should file an issue for this :thinking_face:
It certainly looks like a bug, maybe even 2 as the second error message doesn't look like something I would expect considering your configuration file.
Could you create a bug in our tracker: https://github.com/quarkusio/quarkus/issues/new?assignees=&labels=bug&template=bug_report.md&title= ?
It would be nice if you could provide a reproducer. AFAICS, Quarkus doesn't start at all so probably your pom.xml with the various extensions you use and your configuration file should be enough to reproduce the issue.
Turns out this was not an issue. I just had an old Gradle config; some things changed with the Gradle plugin with version 1.1.0.Final and I just didn't have those.

Spring Boot with spring-cloud: gradle build fails

./gradlew build fails with the error given at the bottom while running :test task. The code just checks if the context is loaded properly.
#RunWith(SpringRunner.class)
#SpringBootTest
#ContextConfiguration
public class RegistryApplicationTests {
#Test
public void contextLoads() {
}
}
The bootstrap.yml file is given below(pretty standard), I'm not sure why it is trying to load the property file from the cloud-config service, how do I work around it??
spring:
application:
name: registry
profiles:
active: default
cloud:
config:
uri: http://localhost:8888
fail-fast: true
eureka:
instance:
prefer-ip-address: true
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
Stack Trace
Caused by: java.lang.IllegalStateException: Could not locate PropertySource and the fail fast property is set, failing
at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:130)
at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:89)
at
....
....
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/registry/default": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:666)
at
....
....
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at
UPDATE
As suggested by #dzatorsky tried adding #Profile("test") and #ActiveProfiles("test"),DID NOT WORK.
Tried manually adding the property file for the test using #TestPropertySource(locations = "file:src/test/resources/application-test.yml") DID NOT WORK
Finally overrode using #TestPropertySource(properties = {"spring.cloud.config.fail-fast=false"}) which worked, but it looks like a very ugly work around
The Inference is bootstrap.yml in src/main/resources overrides the properties specified anywhere else, tried renaming application-test.yml to bootstrap.yml in src/test/resources WORKED.
Is this the cleaner way getting this done?
Error on GET request for "http://localhost:8888/registry/default": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused
Seems that your Spring Cloud Config server is down.
UPDATE: If you want to run your tests without running Config Server (which is a right thing to do in the most cases) then I would suggest doing the following:
Add application-test.yml with the following content:
cloud:
config:
fail-fast: false
Annotate your test class with:
#Profile("test")
In this case, whenever you run your tests they use a default parameters defined in application.yml plus those that you override in application.test.yml.

Spring Boot 1.4: set default profile when deploying the app in Tomcat

I have a Spring Boot 1.4 application which has a YAML-based config file. The config file specifies a default profile: prod
spring:
profiles.active: prod
When I generate a war using Maven and deploy to Tomcat 7, the default profile is not set. Tomcat log:
No active profile set, falling back to default profiles: default
In order to set the default profile in the app deployed in Tomcat, I can pass a System property to Tomcat, like so:
-Dspring.profiles.active=prod
This works, but I would like to use the Spring Boot properties, rather than the System properties for setting a default profile.
Is there a reason why the default yaml property file is ignored when the application is deployed in Tomcat?
Just rename your profile to default and add a different name for development, like this:
spring:
profiles: development
devtools:
restart:
enabled: false
thymeleaf:
mode: HTML
cache: false
http:
multipart:
max-file-size: 3Mb
max-request-size: 4Mb
logging:
level:
org.apache: WARN
org.apache.catalina.webresources.Cache: ERROR
org.hibernate: WARN
org.hibernate.SQL: WARN
org.hibernate.type: WARN
org.hibernate.jpa.internal: ERROR
org.springframework: WARN
org.springframework.data.jpa: WARN
org.springframework.validation: WARN
org.springframework.web: WARN
org.springframework.web.socket: WARN
org.zkoss: WARN
com.ultraip: TRACE
---
spring:
profiles: default
thymeleaf:
mode: HTML
http:
multipart:
max-file-size: 3Mb
max-request-size: 4Mb
messages:
cache-seconds: -1
resources:
cache-period: 259200
server:
port: 80
session:
timeout: 86400
compression:
enabled: true
mime-types: "text/html,text/xml,text/css,text/plain,application/json,application/xml,application/javascript"
security:
headers:
cache: false
logging:
file: "/var/log/intranet.log"
level:
org.apache: WARN
org.apache.catalina.webresources.Cache: ERROR
org.hibernate: WARN
org.hibernate.jpa.internal: ERROR
org.springframework: WARN
org.zkoss: WARN
com.ultraip: INFO
The "---" denotes the start of a different config file

Resources