Annotations
@RestController
The @RestController annotation was introduced in Spring 4.0 to simplify the creation of RESTful web services. It’s a convenience annotation that combines @Controller and @ResponseBody – which eliminates the need to annotate every request handling method of the controller class with the @ResponseBody annotation.
@PathVariable
While @RequestParams extract values from the query string, @PathVariables extract values from the URI path:1
2
3
4
5"/foos/{id}") (
public String getFooById(@PathVariable String id) {
return "ID: " + id;
}
For @PathVariable, we map on path:1
2
3http://localhost:8080/foos/abc
----
ID: abc
1 | "/foos") ( |
For @RequestParam, we map on path:1
2
3http://localhost:8080/foos?id=abc
----
ID: abc
@RequestBody
Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object.
CRUD
POST
1 | "/saveCustomer") ( |
GET
1 | "/search/{theSearchName}") ( |
DELETE
1 | "/delete/{customerId}") ( |
PUT
1 | "/updateCustomer") ( |
Test
For get/delete testing, just input the uri and select the right method.
For put/post testing, besides the steps above:
- Select ‘Body’
- Select ‘raw’
- Select ‘JSON’
- Input data in JSON fashion
- Send request