I'm trying to write a simple Spring Boot application using an in-memory H2 Database. The application needs 3 rest endpoints :
1) Get a list of employees (id, name, surname, gender)
2) Add an employee to the database given an object(name, surname,gender)
3) Update an employee given an employee object(name, surname,gender) with the amended attributes.
I'm still new to spring and databases...
This is what I've got so far
Employee.java
Code:
package com.jason.spring_boot;
/**
* The representational that holds all the fields
* and methods
* @author Jason Truter
*
*/
public class Employee {
private final long id;
private final String name;
private final String surname;
private final String gender;
public Employee(long id, String name, String surname, String gender){
this.id = id;
this.name = name;
this.surname = surname;
this.gender = gender;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
public String getGender() {
return gender;
}
}
EmployeeController.java
Code:
package com.jason.spring_boot;
import java.util.concurrent.atomic.AtomicLong;
/**
* The resource controller handles requests
* @author Jason Truter
*
*/
@Controller
@RequestMapping("/employee")
public class EmployeeController {
private final AtomicLong counter = new AtomicLong();
/**
* GET method will request and return the resource
*/
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody Employee getEmployee(@RequestParam(value = "name", defaultValue = "Stranger") String name,
String surname, String gender){
return new Employee(counter.getAndIncrement(), name, surname, gender);
}
}
Application
Code:
package com.jason.spring_boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}