Spring Path variable & Request Parameter
Path variable
http://localhost:8089/api/v0/hello-world/pathVariable/{var_name}
@PathVariables
annotation is used to extract values from the URI path
@GetMapping(path = "/pathVariable/{var_name}")
public String helloWorldPathVariable(@PathVariable("var_name") String name) {
return String.format("The Value returned is %s", name);
}
@GetMapping("/student/{studentId}")
public ResponseEntity<Student> getStudentById(@PathVariable Long studentId) {
return ResponseEntity.ok(studentService.getStudentById(userId));
}
RequestParam
/jpa/students/pagination?page_size=5&offset=1&sortBy=email
Check the required
and defaultValue
arguments of RequestParam Annotation
// Retrieve all users page by page
@GetMapping(path = "/students/pagination")
public List<Student> retrieveAllUsersPagination(
@RequestParam(defaultValue = "0") Integer offset,
@RequestParam(value = "page_size",required = false, defaultValue = "10") Integer pageSize,
@RequestParam(defaultValue = "id") String sortBy) {
...
}
defaultValue with GET request
If Requirement for the API is to get the summary of recent orders in the last 24 hours (without any request params)
localhost:8084/orders/dashboard/summary
OR
pass a specific date to search
localhost:8084/orders/dashboard/summary?fromDate=2021-06-01&toDate=2022-06-17
Notice the defaultValue
property of @RequestParam
. This is useful when the
dates are not passed.
The fromDate
param takes the current date as default date
#{T(java.time.LocalDateTime).now()}
toDate
parameter takes the end of the day today as the default vale(
#{T(java.time.LocalDateTime).now().plusDays(1)}
)
If, however, explicit dates are passed and particular type of Order is also passed in request parameter
localhost:8084/orders/dashboard/details?fromDate=2021-06-01&toDate=2022-06-17&orderType=Grocery Order
Case : Date without time
v1/customer/{customerId}/orders?orderDate=10-11-2021
The difficulty is in taking the date in a particular format and parse it. Only Date is involved and not the time field.
Notice the data format and required parameter