Basic Spring 5.0 - Lesson07 - Spring RESTful
Basic Spring 5.0 - Lesson07 - Spring RESTful
Services
Basic Spring 5.0
Lesson Objectives
Dispatcher CLient
CLient Controller
Servlet
Response via @ResponseBody
Controller
Response
View
@Controller
@RequestMapping("employees")
public class EmployeeController {
employee.setName(name);
employee.setEmail("employee1@genuitec.com");
return employee;
employee.setName(name);
employee.setEmail("employee1@genuitec.com");
return employee;
}
7.3 Life cycle of a Request in Spring MVC Restful
HandlerMapping
(@RequestMapping)
DispatcherServlet RestController
HttpRequest HttpResponse
Client
7.4 Why REST Controller ?
Traditional Spring MVC controller and the RESTful web service controller differs in the way the HTTP
response body is created
RESTful controller simply returns the object and the object data is written directly to the HTTP
response as JSON/XML
Request Handler
Dispatcher Mapping
Servlet
CLient
Response
RESTController
(@Controller +
@ResponseBody)
7.6 Spring 4 support for RESTful web services
@RestController
@RequestMapping("/service/greeting")
public class SpringRestController {
@RequestMapping(value = "/{name}", method = RequestMethod.GET)
public String sayHello(@PathVariable Optional<String> name) {
String result = “Welcome " + name + " to Spring session!!!";
return result;
} No “name” required. “Do
} not repeat yourself” with
Java 8 version
output
REST Controller
@RestController
@RequestMapping("employees")
public class EmployeeController {
employee.setName(name);
employee.setEmail("employee1@genuitec.com");
return employee;
employee.setName(name);
employee.setEmail("employee1@genuitec.com");
return employee;
}
RESTful URLs – HTTP methods
http://localhost:9090/SpringRESTWebServices/rest/co
untries
client Fetching all
country details –
HTTP Get
http://localhost:9090/SpringRESTWebServices/rest/countries/new
Creating a
client new country
– HTTP Post
http://localhost:9090/SpringRESTWebServices/rest/countrie
Deleting an
s/newDel
existing
client country –
HTTP
Delete
7.8 Cross-Origin Resource Sharing (CORS)
@CrossOrigin(origins = "http://localhost:4200")
@RestController
public class CountryController {
@Autowired
ICountryService service;
//@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping(value = "/countries/search/{id}",method =
RequestMethod.GET,headers="Accept=application/json")
public Country getCounty(@PathVariable int id) {
return service.searchCountry(id);
}}
6.9 @RequestBody annotation
@RestController
public class EmployeeController {
@Autowired
IEmployeeService empservice;
@RequestMapping(value ="/employee/create/", consumes =
MediaType.APPLICATION_JSON_VALUE,
headers="Accept=application/json",method =
RequestMethod.POST)
public List<Employee> createEmployee(@RequestBody Employee
emp) {
empservive.addEmployee(emp);
return empservice.getAllEmployee();
}}
Demo: SpringRESTDemos
SpringRESTDemo
Summery
REST Controller
Lab 2
Review Question