Search code examples
javastringapache-camelquarkusapplication.properties

Enter variable within a string found in the applitcation.properties


Good afternoon.

I am currently working with quarkus and apache camel and I need to return a controlled error for the different http codes, in the case of 404 I need to return the following message that I have configured as a variable in the application.properties:

enter image description here

Basically my service receives a mobile number as an input parameter and what I want to do is where the value of %1 is, that is, if I pass the number 5842410000 and it does not exist, return an error like the following:

Resource 5842410000 does not exist

I know that I can get the value of the properties in my mappign class as follows:

private final String exceptionText = ConfigProvider.getConfig().getValue("error.404.exceptionText", String.class);

The issue is how can I make it dynamic to insert the value of the input field into the error message


Solution

  • You can have the %s placeholder in your application.properties file

    error.404.exceptionText=Resource %s does not exist
    

    And you can dynamically format the exceptionText with phone.

    private final String exceptionText = ConfigProvider.getConfig().getValue("error.404.exceptionText", String.class);
    
    ...
    
    String dynamicExceptionText = String.format(exceptionText, phoneNumber);
    

    Now %s placeholder will have phoneNumber value.