Generate Random values in Spring boot


In this tutorial we will look into the ways of generating a random string or integer values in Spring boot:

Find this project on Github

https://github.com/javacodenet/random-generator/


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javacodenet</groupId>
<artifactId>spring-boot</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
view raw pom.xml hosted with ❤ by GitHub

Application.java

package com.javacodenet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@SpringBootApplication
public class Application {
@Autowired
RandomValues randomValues;
@RequestMapping("/random")
public String randomMessage() {
return "Your random string message is: " + randomValues.getRandomStringMessage()
+ ", Your random integer value is: " + randomValues.getRandomInteger()
+ ", Your random integer value with 50 to 500 range: " + randomValues.getRandomIntegerWithRange()
+ ", Your random integer value with 1 to 10 range : " + randomValues.getRandomIntegerWithSpecialRange();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

RandomValues.java

Bean with random values injected from the configuration file.
package com.javacodenet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class RandomValues {
@Value("${randomStringMessage}")
private String randomStringMessage;
@Value("${randomInteger}")
private String randomInteger;
@Value("${randomIntegerWithRange}")
private String randomIntegerWithRange;
@Value("${randomIntegerWithSpecialRange}")
private String randomIntegerWithSpecialRange;
public String getRandomStringMessage() {
return randomStringMessage;
}
public void setRandomStringMessage(String randomStringMessage) {
this.randomStringMessage = randomStringMessage;
}
public String getRandomInteger() {
return randomInteger;
}
public void setRandomInteger(String randomInteger) {
this.randomInteger = randomInteger;
}
public String getRandomIntegerWithRange() {
return randomIntegerWithRange;
}
public void setRandomIntegerWithRange(String randomIntegerWithRange) {
this.randomIntegerWithRange = randomIntegerWithRange;
}
public String getRandomIntegerWithSpecialRange() {
return randomIntegerWithSpecialRange;
}
public void setRandomIntegerWithSpecialRange(String randomIntegerWithSpecialRange) {
this.randomIntegerWithSpecialRange = randomIntegerWithSpecialRange;
}
}

application.properties

A configuration file containing random value definitions.
server.context-path=/randomGenerator
randomStringMessage = ${random.value}
randomInteger = ${random.int}
#Generates random integer from 50 to 499, 500 is exclusive, don't add any spaces between values
randomIntegerWithRange = ${random.int(50,500)}
#paranthesis can be replaced with special characters
randomIntegerWithSpecialRange = ${random.int#1,10#}

Output


Comments

Popular posts from this blog

Generate PDF From XML Or Java Object Using Apache FOP

Generate/Read an Excel file in Java using Apache POI