How can I ingore placeholders in property file in spring boot? - spring-boot

I have urls like localhost:8080/boot/${task}/say
in property file, I am reading this url in my code using poprerty placeholder, problem is here spring boot trying to search placeholder in url. I want to ingore that and send complete url with that placeholder into response.
I have already declared bean of PropertySourcePlaceholderConfigure and setIgoreUnresolvablePlaceholders as true but still it asking for value of placeholder.

You need to escape $ sign.
localhost:8080/boot/#{'$'}{task}/say

Related

Spring Boot | parsing Url with query params in properties file with URIComponentBuilder

I have a URL in my application-local.properties file which looks like
basePath: http://host.com/
endpoint: /endpoint?q={value1}
I am mapping it to variables in a class annotated with #ConfigurationProperties. I am using URLComponentBuilder to build the full URL like this:
UriComponentsBuilder.fromUriString(basePath).path(properties.endpoint).build(123)
The URL I am getting is this: http://host.com/endpoint%3Fq=123 i.e. the ? gets encoded.
The question is that I don't want to remove query params from the properties file and add them only in the code where I am building the URL. I want to keep the full URI endpoint in the properties file and still be able to generate the correct (without ? being encoded) full URL through URIComponentBuilder

Custom application property to be supplied to spring boot app through cmd line

I was wondering if we can supply a custom attribute (a key to be in application.properties file), I know for sure that -Dserver.port=8080 works, and overrides the property value, but server.port is a spring boot's expected property value.
How about something other than that, for example a jdbc connection string or service name? does -Ddb.service.name=dbservice work?
Yes, any property can be set via system property. You can use -D or -- notation. There are also a variety of property sources Spring Boot uses:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

What is exactly server.error.path property?

In Spring Boot, what is the purpose of server.error.path property in application.properties file?
The documentation just says:
Path of the error controller
But I want a clear description of this property with an example.
server.error.path - used as part of url for error pages.
site.getBaseUrl() + "/error"
For example some error happen on server side and you decide redirect user to error page like this:
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/images/custom-error-page-aws-404-example.png
Code example of error controller you can find here:
https://www.logicbig.com/tutorials/spring-framework/spring-boot/implementing-error-controller.html
You can use this property in #RequestMapping("/error"). But instead of "/error" you can use "${server.error.path}"
UPDATE:
Also, Spring Boot BasicErrorController use server.error.path property
Property server.error.path in spring boot application used to define an error path while dealing with custom error handler. In Spring we create custom error handler using functional interface ErrorController, ths interface has a String type method getErrorPath which helps us to return the error page path(our error page as view).
But from Spring 2.3.0 this getErrorPath() method has been deprecated and replaced with server.error.path to manage the error path.
e.g. server.error.path=/error
For more detail about interface ErrorController, please refer Spring doc for ErrorController

How to get Spring property with embedded property variable as a literal

I currently have a Spring application.properties with 2 properties defined as follows:
validator.url=http://location.com/${domain}/${family}
query.validator.url=${validator.url}
Currently my application resolves query.validator.url to be ${validator.url}
is there anyway that I can have it resolve to the same value as validator.url which is http://location.com/${domain}/${family}
Note: ${domain} and ${family} don't resolve, they are handled in code.
Try the following:
validator.url=http://location.com/#{'$'}{domain}/#{'$'}{family}
query.validator.url=${validator.url}
Both validator.url and query.validator.url will resolve to http://location.com/${domain}/${family}
Alternatively, you can create a property for the $, which you can then use inside validator.url, e.g.:
var=$
validator.url=http://location.com/${var}{domain}/${var}{family}
There's an issue regarding this on Spring's JIRA

Why is plus(+) decoded to space ( ) in url path with springboot rest controller?

When calling GET http://localhost:8080/things/ZhaD2lk27XQPRJtwrABltd+UTWXcbnY%2FTrpxGP7VDVo= my Spring Boot application RestController with a request handler like this:
#RequestMapping("/things/{thingId}")
public ResponseEntity<Thing> getThing(
#PathVariable String thingId) {
System.out.println("thingId=" + thingId);
...
results in the following being printed ZhaD2lk27XQPRJtwrABltd UTWXcbnY/TrpxGP7VDVo= instead of what I would have expected ZhaD2lk27XQPRJtwrABltd+UTWXcbnY/TrpxGP7VDVo=.
As you can see, the plus is being turned into a space. This should not happen with the path part, only the query part. This is why the Spring UriComponentsBuilder.build().encode() I'm using to build the URL doesn't turn the plus into %2B.
I needed to tweak the application already to get the encoded slash (/) to work. See REST Endpoint unreachable if ID in URL contains %2F for details.
I'm using SpringBoot 1.4.4.RELEASE which uses Tomcat embed 8.5.11.
I have tried calling the service from Spring RestTemplate, Postman and Chrome. Same results in all cases, the plus is turned into a space
I was able to resolve after identifying that my IDE had automagically added spring-boot-starter-undertow to the POM file. I did not exclude spring-boot-starter-tomcat from spring-boot-starter-web so I'm not sure what was happening under the covers but removing the spring-boot-starter-undertow dependency fixed the issue.

Resources