Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping Annotation with Examples





Hey guys in this post, we will discuss spring @GetMapping, @PostMapping, @PutMapping, @PatchMapping and @DeleteMapping annotation with examples

Overview


These annotations have been introduced in the spring 4.3version. These annotations will map the HTTP web requests to the specific handler methods.

@GetMapping annotation


Annotation for mapping HTTP GET requests onto specific handler methods. Specifically, @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).

It takes the following optional elements

  • consumes – Narrows the primary mapping by media types that can be consumed by the mapped handler.
  • headers  – The headers of the mapped request, narrowing the primary mapping.
  • name – Assign a name to this mapping.
  • params – The parameters of the mapped request, narrowing the primary mapping.
  • path – The primary mapping expressed by this annotation.
  • produces – Narrows the primary mapping by media types that can be produced by the mapped handler.
  • value – The primary mapping expressed by this annotation.
@GetMapping("/customers")
public ResponseEntity<List<Customer>> getAllCustomers() {
	try {
		List<Customer> list = customerRepo.findAll();
		if (list.isEmpty() || list.size() == 0) {
			return new ResponseEntity<List<Customer>>(HttpStatus.NO_CONTENT);
		}
		
		return new ResponseEntity<List<Customer>>(list, HttpStatus.OK);
	} catch (Exception e) {
		return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
	}
}

@PostMapping annotation


Annotation for mapping HTTP POST requests onto specific handler methods. Specifically, @PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.POST).

It takes the following optional elements

  • consumes – Narrows the primary mapping by media types that can be consumed by the mapped handler.
  • headers  – The headers of the mapped request, narrowing the primary mapping.
  • name – Assign a name to this mapping.
  • params – The parameters of the mapped request, narrowing the primary mapping.
  • path – The primary mapping expressed by this annotation.
  • produces – Narrows the primary mapping by media types that can be produced by the mapped handler.
  • value – The primary mapping expressed by this annotation.
@PostMapping("/customers")
public ResponseEntity<Customer> save(@RequestBody Customer customer) {
	try {
		return new ResponseEntity<>(customerRepo.save(customer), HttpStatus.CREATED);
	} catch (Exception e) {
		return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
	}
}

@PutMapping annotation


Annotation for mapping HTTP PUT requests onto specific handler methods. Specifically, @PutMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.PUT).




It takes the following optional elements

  • consumes – Narrows the primary mapping by media types that can be consumed by the mapped handler.
  • headers  – The headers of the mapped request, narrowing the primary mapping.
  • name – Assign a name to this mapping.
  • params – The parameters of the mapped request, narrowing the primary mapping.
  • path – The primary mapping expressed by this annotation.
  • produces – Narrows the primary mapping by media types that can be produced by the mapped handler.
  • value – The primary mapping expressed by this annotation.
@PutMapping("/customers/{id}")
public ResponseEntity<Customer> updateCustomer(@RequestBody Customer customer) {
	
	try {
		return new ResponseEntity<Customer>(customerRepo.save(customer), HttpStatus.OK);
	} catch (Exception e) {
		return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
	}
}

@DeleteMapping annotation


Annotation for mapping HTTP DELETE requests onto specific handler methods. Specifically, @DeleteMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.Delete).

It takes the following optional elements

  • consumes – Narrows the primary mapping by media types that can be consumed by the mapped handler.
  • headers  – The headers of the mapped request, narrowing the primary mapping.
  • name – Assign a name to this mapping.
  • params – The parameters of the mapped request, narrowing the primary mapping.
  • path – The primary mapping expressed by this annotation.
  • produces – Narrows the primary mapping by media types that can be produced by the mapped handler.
  • value – The primary mapping expressed by this annotation.
@DeleteMapping("/customers/{id}")
public ResponseEntity<HttpStatus> deleteCustomer(@PathVariable Long id) {
	try {
		Optional<Customer> customer = customerRepo.findById(id);
		if (customer.isPresent()) {
			customerRepo.delete(customer.get());
		}
		return new ResponseEntity<HttpStatus>(HttpStatus.NO_CONTENT);
	} catch (Exception e) {
		return new ResponseEntity<HttpStatus>(HttpStatus.INTERNAL_SERVER_ERROR);
	}
}

@PatchMapping annotation


Annotation for mapping HTTP PATCH requests onto specific handler methods. Specifically, @PatchMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.PATCH).

It takes the following optional elements

  • consumes – Narrows the primary mapping by media types that can be consumed by the mapped handler.
  • headers  – The headers of the mapped request, narrowing the primary mapping.
  • name – Assign a name to this mapping.
  • params – The parameters of the mapped request, narrowing the primary mapping.
  • path – The primary mapping expressed by this annotation.
  • produces – Narrows the primary mapping by media types that can be produced by the mapped handler.
  • value – The primary mapping expressed by this annotation.
@PatchMapping("/customers/{id}/{name}")
public ResponseEntity<Customer> updateCustomerName(@PathVariable Long id, @PathVariable String name) {
	try {
		Customer customer = customerRepo.findById(id).get();
		customer.setName(name);
		return new ResponseEntity<Customer>(customerRepo.save(customer), HttpStatus.OK);
	} catch (Exception e) {
		return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
	}
}




Partners: euteller kasinot ilman rekisteröitymistä online casino uden rofus skrill guthaben auszahlen neteller online casinos casino universe erfahrungen sweet bonanza rtp casino ohne lizenz vergleich test book fra gratis casino trustly utan svensk licens uudet netti kasinot

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply