Spring Boot : Configuring Random values
I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
Search for a command to run...
I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
No comments yet. Be the first to comment.
Spring AI Tool Calling: From Chatbot to AI Agent with @Tool Your AI is smart. It knows an enormous amount. But it's frozen. It doesn't know what time it is right now. It doesn't know what's on your ca
Spring AI Advisors API Explained Series: Spring AI Complete Course — Lecture 4 of 12Reading Time: 8 minutesLevel: Intermediate Most developers stop at ChatClient. That is enough for demos. It is not
Working with Multiple AI Models in Spring AI Spring AI Complete Course — Lecture 3 of 12Previous: Lecture 2 — ChatClient API | Next: Lecture 4 — Advisors API In production AI applications, you rarel
Spring AI ChatClient API: The Fluent Heart of AI Integration Introduction If you've ever tried integrating AI models into a Java application, you know the pain. HTTP clients, API keys scattered everywhere, vendor-specific SDKs that never quite fit. W...
What is Spring AI? — Why Java Developers Need This in 2026 Every AI tutorial you see is in Python. LangChain, LlamaIndex, OpenAI SDK — all Python. But here's the uncomfortable truth: 80% of enterprise backends run Java. So who's building AI into thos...
Many times we need to get random values ,for example you need to generate random pin for verification or a some random value for email verification. Spring provides easy way to do it.
RandomValuePropertySource is used to inject random values. RandomValuePropertySource can produce integers, longs, uuids or strings.
In application.properties we will add following :
app.random=${random.value}
my.random.integer=${random.int}
my.random.long=${random.long}
my.random.uuid=${random.uuid}
my.random.less.than.hundred=${random.int(100)}
my.random.within.range=${random.int[1000,9999]}
|
The above values are self explanatory . They are creating random values. In order to see if they work as expected let’s create a RequestMapping so that we access these value and display on UI /POSTMAN.
We will inject above defined properties via @Value annotation. Doing so we can also see that spring will keep the type safety in consideration. If we take a Integer value to inject an integer it will inject integer , Long for long and so on . Let’s create another controller CommonController as below with a RequestMapping.
@RequestMapping("v1")
public class CommonController {
@Value("${app.random}")
private String random;
@Value("${app.random.integer}")
private Integer randomInteger;
@Value("${app.random.long}")
private Long randomLong;
@Value("${app.random.uuid}")
private String randomUUID;
@Value("${app.random.less.than.hundred}")
private Integer randomLessThanHundred;
@Value("${app.random.within.range}")
private Integer randomWithinRange;
@RequestMapping("random-values")
protected MapgetRandomValues(){
Map randomValues= new LinkedHashMap();
randomValues.put("random",random);
randomValues.put("randomInteger",randomInteger);
randomValues.put("randomLong",randomLong);
randomValues.put("randomUUID",randomUUID);
randomValues.put("randomLessThanHundred",randomLessThanHundred);
randomValues.put("randomWithinRange",randomWithinRange);
return randomValues;
}
}
Here we can observe that we are using a Map to collect all values in our method and send them back. Once we hit http://localhost:8082/v1/random-values (Considering you are still giving port as command line parameter i.e.–server.port=8082) we will get following response.
{
“random”: “2b26227a3a78bbbac96617f094b613e6”,
“randomInteger”: 728349517,
“randomLong”: -529828434181910766,
“randomUUID”: “c5b8aa23-5992-48c3-9aed-f7b9e0e223c7”,
“randomLessThanHundred”: 5,
“randomWithinRange”: 7208
}
With above value , we can be assured that our RandomValuePropertySource is correctly working.