Starting with Spring Boot Rest

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId></dependency>

Rest Controller

The @RestController annotation is used to define the RESTful web services. It serves JSON, XML and custom response. Its syntax is shown below −

@RestController
public class EmployeeController {
}

Request Mapping

The @RequestMapping annotation is used to define the Request URI to access the REST Endpoints. We can define Request method to consume and produce object. The default request method is GET.

@RequestMapping(value="/employees")
public ResponseEntity<Object> getEmployees() { }

Request Body

The @RequestBody annotation is used to define the request body content type.

public ResponseEntity<Object> createEmployee(@RequestBody Product product) {
}

Path Variable

The @PathVariable annotation is used to define the custom or dynamic request URI. The Path variable in request URI is defined as curly braces {} as shown below −

@GetMapping("/getEmpNo/{empNo}")
public Employee getEmployee(@PathVariable("empNo") String empNo) {
System.out.println("Data Fetched Successfully");return employeeDao.getEmployee(empNo);
}

Request Parameter

The @RequestParam annotation is used to read the request parameters from the Request URL. By default, it is a required parameter. We can also set default value for request parameters as shown here −

public ResponseEntity<Object> getEmployee(
@RequestParam(value = "name", required = false, defaultValue = "honey") String name) {
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store