Spring boot Post Method Example with JSON and Postman.

Spring boot Post Method Example with JSON and Postman.

This article helps you to understand the POST method of Spring Boot. Firstly you have to install IntelliJ Idea and Postman as prerequisites and it is very helpful if you can read my previous article related to the GET method.

This is my scenario. I want to save a student to the database using an API, His name, age, gender to be saved.
Below steps help you to build the application.

This is the structure of a project. as I mentioned in previous articles you should aware of the structure of the project.

Below is the pom file.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath/> <!-- lookup parent from repository -->
	<description>test app</description>






Then create a Controller class and past this code or you can write. This is the same as the GET method example but there are a few differences. You can see that I used a Student class to map the JSON.

package com.example.learnspringboot.Controller;

import com.example.learnspringboot.Dto.Student;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

public class PostTestController {

    @RequestMapping(value = "/test/postmethod/sayhello", method = RequestMethod.POST, consumes = "application/json")
    public String getData(@RequestBody Student student) {
        return "Student Name is :" + student.getName() + " Age : " + student.getAge() + " Gender is : " + student.getGender();

Then create a Student (DTO) class – It is used to map the corresponding JSON data to Java Object. All JSON property names should equal to variable names in the DTO class. Then spring boot will automatically map the data.

package com.example.learnspringboot.Dto;

public class Student {

    private String name;
    private String gender;
    private int age;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public String getGender() {
        return gender;

    public void setGender(String gender) {
        this.gender = gender;

    public int getAge() {
        return age;

    public void setAge(int age) {
        this.age = age;

Finally, you can send the request using this Sample JSON Payload through Postman and you will be able to understand the POST method of Spring Boot.

	"name" : "Sarah",
	"gender": "Female",
	"age" : "29"

Git Location – https://github.com/rajithabhanuka/Learnspringboot.git