✍️
Notes.md
  • Table of contents
  • React.Js
    • React Hooks
    • Old :- React : Using Classes
  • Blockchain
    • Solidity
    • Custom ERC20 token
    • Contract
  • Tools and Tech
    • Docker
    • Git version Control
  • Java
    • Data & Data Types
    • IO in Java
    • Data Structures
      • Array in Java
      • Collections in Java
      • Map in Java
      • Enums in Java
      • Linked List in Java
      • List in Java
      • Queues & Stacks
      • Set in Java
      • TreeSet and TreeMap
    • Object Oriented Programming
      • Object Class Methods and Constructor
      • Immutable Class & Objects
      • Constructors
      • Visibility
      • Generics
    • Threads in Java
    • Useful Stuff Java
      • Lambda & Stream
    • Keywords in Java
      • Annotations
      • Comparators
      • Packages in Java
    • Miscellaneous
    • Articles to refer to
  • Golang
    • Competitive Programming in Go
    • Testing simple web server
    • Learning Go : Part 1
    • Maps vs slices
    • Golang Garbage Collector 101
    • Things Golang do differently
    • Go Things
  • Linux
    • Shell programming
    • Linux Commands Part 1 - 4
    • Linux Commands Part 5 - 8
    • Linux Commands Part 9 - 10
  • Software Design
    • Solid Design
    • OOPS
    • Design Patterns
      • Creational Design Pattern
        • Builder DP
        • Factory DP
        • Singleton DP
      • Adapter DP
      • Bridge DP
      • Iterator DP
      • State DP
      • Strategy DP
      • Behavioral Design Pattern
        • Observer DP
      • Structural Design Pattern
        • Facade DP
  • Cloud
    • Google Cloud Platform
      • GCP Core Infrastructure
      • Cloud Networking
  • Spring Boot
    • Spring Basics
      • Spring Beans
      • Important Annotations
      • Important Spring Things
      • Maven Things
      • Spring A.O.P
    • Spring Boot Controller
      • Response Entity Exception Handling
    • Spring Things
    • Spring MVC
    • Spring Data
      • Redis
      • Spring Data JPA
      • JDBC
    • Apache Camel
  • Miscellaneous
    • Troubleshooting and Debugging
Powered by GitBook
On this page
  • @Controller
  • @RestController
  • @RequestMapping
  • @RequestBody
  • @PathVariable
  • @PathVariable
  • @RequestParam
  • HTTP Mapping
  • @ResponseEntity

Was this helpful?

  1. Spring Boot

Spring Boot Controller

@Controller

A simple version to annotate a class as controller

  • You need to annotate @ResponseBody in 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

Is 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

Helps the server to route requests to the controller classes specified in string parameter

@RequestMapping(
  value = "/ex/foos", 
  method = RequestMethod.GET, 
  produces = "application/json"
)
  • Fallback for all requests that didn't match

@RequestMapping(value = "*", method = RequestMethod.GET)

@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

@GetMapping("/{id}", produces = "application/json")
public @ResponseBody Book getBook(@PathVariable int id) 
{

}

@PathVariable

Used endpoint that identifies an entity with a primary key:

@GetMapping("/api/{id}")
public String getEmployeesById(@PathVariable String id) 
{

}

@PathVariable

Can be used to handle template variables in the request URI mapping

@GetMapping("/api/employees/{id}")
@ResponseBody
public String getEmployeesById(@PathVariable("id") String id) {
    return "ID: " + id;
}

-- Multiple PathVariable

@GetMapping("/api/employees/{id}/{name}")
@ResponseBody
public String getEmployeesByIdAndName(
    @PathVariable String id, @PathVariable String name) 
{

}

-- Capture PathVariable in MapString

@GetMapping("/api/employeeswithmapvariable/{id}/{name}")
@ResponseBody
public String getEmployeesById(
    @PathVariable Map<String, String> pathVarsMap) 
{

}

    
-- when PathVariable isnt required 

@GetMapping(value = { "employees/", "employees/{id}" })
@ResponseBody
public String getEmployeesById(@PathVariable(required = false) String id) 
{

}

@RequestParam

@PostMapping("/api/foos")
@ResponseBody
public String addFoo(@RequestParam(name = "id") String fooId, @RequestParam String name) 
{ 

}

http://localhost:8080/api/foos?id=1&name=uday

-- Multiple RequestParam 
@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam List<String> id) {

}

http://localhost:8080/api/foos?id=1,2,3

HTTP Mapping

-- @GetMapping("/get")

public @ResponseBody ResponseEntity<String> get() {
    return new ResponseEntity<String>("GET Response", HttpStatus.OK);
}

-- @PostMapping
@PostMapping("/post")
public @ResponseBody ResponseEntity<String> post() {
    return new ResponseEntity<String>("POST Response", HttpStatus.OK);
}

-- @PutMapping
@PutMapping("/put")
public @ResponseBody ResponseEntity<String> put() {
    return new ResponseEntity<String>("PUT Response", HttpStatus.OK);
}
-- @DeleteMapping
@DeleteMapping("/delete")
public @ResponseBody ResponseEntity<String> delete() {
    return new ResponseEntity<String>("DELETE Response", HttpStatus.OK);
}

-- @PatchMapping
@PatchMapping("/patch")
public @ResponseBody ResponseEntity<String> patch() {
    return new ResponseEntity<String>("PATCH Response", HttpStatus.OK);
}

@ResponseEntity

ResponseEntity 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 null

  • takes

    • HTTPStatusCode

    • Object to be returned

@GetMapping("/hello")
ResponseEntity<String> hello() {
    return new ResponseEntity<>("Hello World!", HttpStatus.OK);
}
PreviousSpring A.O.PNextResponse Entity Exception Handling

Last updated 2 years ago

Was this helpful?