Spring Boot Controller
@Controller
@ControllerA simple version to annotate a class as controller
You need to annotate
@ResponseBodyin front of the return type of member function.
@Controller
@RequestMapping("books")
public class SimpleBookController {
@GetMapping("/{id}", produces = "application/json")
public @ResponseBody Book getBook(@PathVariable int id) {
}
}@RestController
@RestControllerIs a specialised version of the controller. It includes the @Controller and @ResponseBody annotation.
No need to annotate return type with
@ResponseBody
@RestController
@RequestMapping("books-rest")
public class SimpleBookRestController {
@GetMapping("/{id}", produces = "application/json")
public Book getBook(@PathVariable int id) {
}
}@RequestMapping
@RequestMappingHelps the server to route requests to the controller classes specified in string parameter
Fallback for all requests that didn't match
@RequestBody
@RequestBody@RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic de-serialisation. Spring automatically de-serialises the JSON into a Java type
@PathVariable
@PathVariableUsed endpoint that identifies an entity with a primary key:
@PathVariable
@PathVariableCan be used to handle template variables in the request URI mapping
@RequestParam
@RequestParamHTTP Mapping
@ResponseEntity
@ResponseEntityResponseEntity represents the whole HTTP response: status code, headers, and body. As a result, we can use it to fully configure the HTTP response.
mention the return type of a function as
ResponseEntity<T>, cannot be nulltakes
HTTPStatusCodeObject to be returned
Last updated
Was this helpful?