top of page

4 Ways to send data using API with Spring Boot annotation

Jay Patel
  1. @PathVariable

  2. @RequestParam

  3. @RequestHeader

  4. @RequestBody


Path Variable

Path variable is not secure as it exposes data. Also, It is limited in size to 2048 characters.



@PostMapping("/{id}/{firstName}/{lastName}")

public Student findStudentById(@PathVariable Integer id, @PathVariable String firstName, @PathVariable String lastName) {
		
		return studentServiceImpl.addStudent(id, firstName, lastName);

}
		


Request Parameter

Request parameter is also not secure as it exposes data. Also, it is limited in size to 2048 characters.



@PostMapping("/param")	

public Student addStudentByRequestParam(@RequestParam Integer id, @RequestParam String firstName, @RequestParam String lastName) {

	return studentServiceImpl.addStudent(id, firstName, lastName);	

}


Request Header

Request header is secure, but it is mainly used for content type, languages, locale, and authorization purpose.



@PostMapping("/header")

public Student addStudentByRequestHeader(@RequestHeader("id") Integer id, @RequestHeader("firstName") String firstName, RequestHeader("lastName") String lastName) {

		return studentServiceImpl.addStudent(id, firstName, lastName);	

}
		


Request Body

Request body is widely used to send data as it accepts JSON and XML format. We can pass object in JSON format using request body and it is easy to parse into object. Also, it has no size restriction and it is secure.



@PostMapping("/body")

public Student addStudentByRequestBody(@Valid @RequestBody Student student) {

		return studentServiceImpl.addStudent(student);
}
		

 
 
 

Recent Posts

See All

Commentaires


bottom of page