-
Notifications
You must be signed in to change notification settings - Fork 10
QBit Resourceful RESTful Microservices
You can build resourceful REST URIs using @RequestMapping.
Typically you use HTTP GET to get a list of something and if you are returning more than one then the
URI path should end with /
as follows:
@RequestMapping("/department/")
public List<Department> getDepartments() {
To add to a list, you would use a PUT or a POST. PUT is generally used for updates, and POST is used to create. If you were editing an object at a give ID you would use a PUT, but if you were adding a new item to a list, you would use a PUT to update a list or a POST to create an item. Dealers choice.
@RequestMapping(value = "/department/{departmentId}/", method = RequestMethod.POST)
public boolean addDepartment(@PathVariable("departmentId") Integer departmentId,
final Department department) {
You could easily get into a long drawn out argument about which to use PUT or POST in the above scenario, because you are updating the list (adding an item to it), but you are creating a department. Just remember POST for create, and PUT for update.
To get a single employee, you could use a path param. Some say that path params are nice for people and search engines, I say ok dude.
Using a path param @RequestMapping(value = "/department/{departmentId}/employee/{employeeId}", method = RequestMethod.GET)
@RequestMapping(value = "/department/{departmentId}/employee/{employeeId}", method = RequestMethod.GET)
public Employee getEmployee(@PathVariable("departmentId") Integer departmentId,
@PathVariable("employeeId") Long employeeId) {
HTTP GET is the default, but you can specify it as we did above.
There are other annotations that work the same way except the HTTP method is in the name of the param.
- @POST
- @PUT
- @GET
You could rewrite the last examples like this.
@GET("/department/")
public List<Department> getDepartments() {
...
@POST(value = "/department/{departmentId}/")
public boolean addDepartment(@PathVariable("departmentId") Integer departmentId,
final Department department) {
...
@GET(value = "/department/{departmentId}/employee/{employeeId}")
public Employee getEmployee(@PathVariable("departmentId") Integer departmentId,
@PathVariable("employeeId") Long employeeId) {
Making the method name also be the annotation name makes things a bit more compact, and a bit easier to read.
Here are some more Resourceful REST examples in QBit to ponder.
package com.mammatustech.hr;
import io.advantageous.qbit.annotation.*;
import java.util.*;
@RequestMapping("/hr")
public class HRService {
final Map<Integer, Department> departmentMap = new HashMap<>();
@RequestMapping("/department/")
public List<Department> getDepartments() {
return new ArrayList<>(departmentMap.values());
}
@RequestMapping(value = "/department/{departmentId}/", method = RequestMethod.POST)
public boolean addDepartment(@PathVariable("departmentId") Integer departmentId,
final Department department) {
departmentMap.put(departmentId, department);
return true;
}
@RequestMapping(value = "/department/{departmentId}/employee/", method = RequestMethod.POST)
public boolean addEmployee(@PathVariable("departmentId") Integer departmentId,
final Employee employee) {
final Department department = departmentMap.get(departmentId);
if (department == null) {
throw new IllegalArgumentException("Department " + departmentId + " does not exist");
}
department.addEmployee(employee);
return true;
}
@RequestMapping(value = "/department/{departmentId}/employee/{employeeId}", method = RequestMethod.GET)
public Employee getEmployee(@PathVariable("departmentId") Integer departmentId,
@PathVariable("employeeId") Long employeeId) {
final Department department = departmentMap.get(departmentId);
if (department == null) {
throw new IllegalArgumentException("Department " + departmentId + " does not exist");
}
Optional<Employee> employee = department.getEmployeeList().stream().filter(
employee1 -> employee1.getId() == employeeId).findFirst();
if (employee.isPresent()){
return employee.get();
} else {
throw new IllegalArgumentException("Employee with id " + employeeId + " Not found ");
}
}
@RequestMapping(value = "/department/{departmentId}/employee/{employeeId}/phoneNumber/",
method = RequestMethod.POST)
public boolean addPhoneNumber(@PathVariable("departmentId") Integer departmentId,
@PathVariable("employeeId") Long employeeId,
PhoneNumber phoneNumber) {
Employee employee = getEmployee(departmentId, employeeId);
employee.addPhoneNumber(phoneNumber);
return true;
}
@RequestMapping(value = "/department/{departmentId}/employee/{employeeId}/phoneNumber/")
public List<PhoneNumber> getPhoneNumbers(@PathVariable("departmentId") Integer departmentId,
@PathVariable("employeeId") Long employeeId) {
Employee employee = getEmployee(departmentId, employeeId);
return employee.getPhoneNumbers();
}
@RequestMapping(value = "/kitchen/{departmentId}/employee/phoneNumber/kitchen/",
method = RequestMethod.POST)
public boolean addPhoneNumberKitchenSink(@PathVariable("departmentId") Integer departmentId,
@RequestParam("employeeId") Long employeeId,
@HeaderParam("X-PASS-CODE") String passCode,
PhoneNumber phoneNumber) {
if ("passcode".equals(passCode)) {
Employee employee = getEmployee(departmentId, employeeId);
employee.addPhoneNumber(phoneNumber);
return true;
} else {
return false;
}
}
}
Read more:
- QBit Microservice Hello World tutorial
- QBit Microservice Hello World Part 2
- QBit Microservice Hello World Health Checks
- QBit Microservice Hello World Stats, Metrics, and Monitoring
- QBit Microservice Reactive programming tutorial
QBit is the Java microservice lib. QBit is a reactive programming lib for building microservices and focuses on JSON, HTTP, WebSocket, and REST. QBit uses reactive programming to build elastic REST, and WebSockets based cloud friendly, web services. SOA evolved for mobile and cloud. ServiceDiscovery, Health, reactive StatService, events, Java idiomatic reactive programming for Microservices.
Reactive Programming, Java Microservices, Rick Hightower
Java Microservices Architecture
[Microservice Service Discovery with Consul] (http://www.mammatustech.com/Microservice-Service-Discovery-with-Consul)
Microservices Service Discovery Tutorial with Consul
[Reactive Microservices] (http://www.mammatustech.com/reactive-microservices)
[High Speed Microservices] (http://www.mammatustech.com/high-speed-microservices)