others-Postman and SpringBoot RESTful api test example

1. The purpose of this post

This post would demo how to use postman to test springboot restful api. I would demo how to use postman to test the restful api(http get/post) step by step.

If you want to use command line test tool(like ab) to test the apis, just refer to this article:

Dependencies:

  • SpringBoot 1.x or 2.x
  • PostMan 7.3.6
  • Jdk 1.8+

2. The api

For example , we developed the following restful api by using springboot:

import com.bswen.sbjh.domain.Student;
import com.bswen.sbjh.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/studentsApi")
public class StudentPageRestController  {
    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/queryByPage", method = RequestMethod.GET)
    public Page<Student> queryByPage(Pageable pageable) {
        Page<Student> pageInfo = studentService.listByPage(pageable);
        return pageInfo;
    }
}

The domain object:

import lombok.Data;
import javax.persistence.*;

@Data
@Entity
@Table(name="tbl_student")
public class Student {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private long id;
    private String name;
    private String branch;
    private int percentage;
    private int phone;
    private String email;

}

The service object:

@Service
public class StudentService {
    @Autowired
    private StudentPageRepository studentPageRepository;

    public Page<Student> listByPage(Pageable pageable) {
        return studentPageRepository.findAll(pageable);
    }
}

The DAO object:

public interface StudentPageRepository extends PagingAndSortingRepository<Student,Long> {
}

And the application.properties file content:

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = test
spring.datasource.password = 123456

And the test data in database: postman-test-restful-api-0

Now we would have a restful url to test: http://localhost:8080/studentsApi/queryByPage

3. How to use postman to test the restful api(http get)–step by step

You can install postman from this site

Then ,open it and create a basic request like this:

3.1 Open postman and click the ‘create’ button

postman-test-restful-api-1

  • At first, click the ‘new’ button
  • Then, click the ‘Get Request’, because we have created an api with GetMapping

3.2 Fill in the form to create the Get Request

postman-test-restful-api-2

Input the request name and save it into a collection , if no collection exists, just create one.

Now we would get an empty postman request: postman-test-restful-api-3

3.3 Send a simple restful get request via postman to springboot service

Now we input the url to test the api: postman-test-restful-api-4

The url is http://localhost:8080/studentsApi/queryByPage?page=2

Click ‘send’ button, then we would get to the breakpoint: postman-test-restful-api-5

As the arrow shows, there are three variables in the http request: size,page and sort

Run across the breakpoint and we would get this result in postman: postman-test-restful-api-6

4. How to use postman to test the restful api(http post json)–step by step

Ok, test springboot http get api is easy with postman, but how to test the http-post-api with postman?

4.1 Add http post api to codes

  • (1) Add this code to controller:
    @PostMapping("/newStudent")
    Student newStudent(@RequestBody Student newStudent) {
        return studentService.save(newStudent);
    }

    @RequestMapping("/student/{id}")
    public Student returnObjectInBrowser(@PathVariable Long id) {
        Student someClass = studentService.findById(id).get();

        return someClass;
    }

It contains two features:

  • it has a new endpoint named ‘/newStudent’ which accepts an json object and call the service to save it
  • it has a new endpoint named ‘/student/{id}’ which accepts an id and return the specified student object as json

  • (2) Add this code to service
    public Student save(Student newStudent) {
        return studentPageRepository.save(newStudent); //insert the new object to database
    }

    public Optional<Student> findById(Long id) {
        return studentPageRepository.findById(id); //query by id and return the result
    }

It just call the Spring data JPA to save and query the Student object.

4.2 Test the post api with postman

  • (1) Create a new http request in postman postman-test-restful-api-7 Make sure to choose the POST http method.

  • (2) Input the post url and headers postman-test-restful-api-8

SpringBoot restful post api needs the following http headers to be present:

  • Content-Type
    • application/json
  • Accept
    • application/json

It means the server needs the client to transfer object as json.

  • (3) Input the http post body Instead of using the http url to pass request body, http post protocol use http body to transfer the request body. postman-test-restful-api-9

If you don’t want to manualy compose the json string, just nagivate to http://localhost:8080/studentsApi/student/1,you would get a json template of the Student class.

  • (4) Click send You would get this reponse: postman-test-restful-api-10

You can see that the new student has been saved to the database and it has an id of 12.

5. Conclusion

As you can see ,using postman to test the springboot restful api is very easy, you can get more help on this topic by visiting this site.