How to set up a Spring-Boot Project
- Create a project using Maven.
- Setting up the parent of the project is the next step. The parent will be spring-boot-starter-parent. Inside the project folder, there will be a file created named pom.xml. Open it, and above the dependencies tag, add the following piece of code.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
- Setting up the starter is the next step. Add the following code under the dependencies tag in pom.xml in order to do this.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- In order to have support for packaging we need to include a maven plugin, and in order to do this, include the following piece of code under the dependencies tag in the pom.xml file.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- The main class of your project must be modified to be a Spring Boot Application. Add the following line of code inside your public static void main method. Please make sure that you change the ClassName word in italics in the code to the name of your main class.
SpringApplication.run(ClassName.class, args);
- Create a folder named model inside your package. Add a class named Employee.java which will contain two attributes id and message, the getters and setters of the two attributes.
private long id;
private String message;
//getter of message
public String getMessage() {
return message;
}
//getter of id
public long getID(){
return id;
}
//setter of message
public void setMessage(String message) {
this.message = message;
}
//setter of id
public void setID(long ID){
this.id=ID;
}
- Create a folder named controller inside your package. Add a class named EmployeeController.java which will contain a method that will handle GET requests.
private final AtomicLong count=new AtomicLong();
@RequestMapping(name = "/employee")
public Employee welcoming(@RequestParam(value = "employeeName", defaultValue = "Worker") String name) {
Employee employee = new Employee();
employee.setMessage("Welcome "+name);
employee.setID(count.incrementAndGet());
return employee;
}
AtomicLong is used because then we can make use of the method incrementAndGet() to get the id attribute incremented whenever a client calls this GET method. If we use something like int, we cannot get this done because the value will not change and increment, but it will always remain the same.
value of @RequestParam is the key of the key-value pair sent along with the GET request, and defaultValue will be the default value placed in the case that the client does not pass a value.
incrementAndGet() method which belongs to the AtomicLong class increments the value and returns it.
We can test this using POSTMAN after running the project by giving the two following commands:
- mvn clean compile package
- java -jar target/<jar file created>.jar
Replace <jar file created> the Executable Jar File created. It will be most probably be created inside a folder called target in your project. If target folder is not there, you do not have to use target/ part in the command.
A better way to do this will be having a separate class for handling the service. In that case, the only task of the controller class will be to redirect requests to the correct service, and give the output.