Demystifying Spring Boot
By: Jordan Williams
Tags: Programming, Java, Spring Boot, API, Software Development

The Basics

Previously, we used request mapping to create a GET request. But there are shorthand syntaxes to achieve the same result. Instead of using @RequestMapping , we could also use @GetMapping .

@GetMapping("/home2")

public String index2(){

return "hello world 2";

}

Let’s add the above code to our controller. This is a shorthand syntax for what we did previously. Similar methods exist for other request types, for example, @PostMapping . After adding this to our code, let’s re-run our application.

If we go to http://localhost:8080/home2, we will see the following output:

hello world 2

We’re now going to do a POST request. This will need a postman.

To create a POST request in Spring Boot, we have to decide what we want in the body of our request. Let’s say we want an employee to be added to a hypothetical database. We want our employee to have an ID, first name, last name, and email address.

A sample request body would look like the snippet below:

{ "eid": 1, "firstName":"jordan", "lastName": "williams", "email": "test@gmail.com" }

Let’s create the methods that would be added to our controller.

@PostMapping("/employee")

public Employee addEmpoyee(@RequestBody Employee employee){

return employee;

}

We are using @PostMapping because it is a POST request. We specify that a parameter is going to be in the body by the @RequestBody annotation. In here we have an employee object. We will have to create an Employee class. This class will have all the attributes that are identical to the ones that are needed to be passed in the body of the request. The same Employee class is the return type of this method. In Spring, objects are turned into JSON when returned. This process is called serialization.

Inside the com.starting.basics package, create the Employee class as shown in the code snippet below:

This is our Employee class.

private int eid;

private String firstName;

private String lastName;

private String email;

These attributes/properties form the blueprint of the body of our request. Your request body needs to have the same names as these attributes or else it won’t be attached to its specific property. The JSON from the request that was sent is turned into the employee object by the serialization process.

The setters are required because this is what binds the values that are passed in the body to the employee object. The getters just allow for easy access if necessary.

Let’s go into Postman and send its request.

Screen share generated by author

Here in Postman, we had to supply all the fields that were stated in the Employee class. It takes all of these fields and stores them in an employee object. If a field is misspelled, it won’t be bind properly to the property in the employee object and that key in the JSON will display null .

Another important part of creating an API is using status codes. Status codes are universally accepted codes that represent actions that happen on the server.

We can create status codes in Spring Boot using the ResponseEntity class. This class has both static and non-static declaration. Both will be shown.

Let's create another get endpoint, /restresponsecodes .

@GetMapping("/restresponsecodes")

public ResponseEntity responses(){



return new ResponseEntity("created response code ", HttpStatus.OK);

}

For this, we returned a response entity. This object takes as its first argument the response message that will be displayed in the response body. In this case, I placed a string. It could easily have been an object that would have been serialized into JSON. The second argument contains the status code, which is represented by the enum class HttpStatus . In this case, we are using the OK constant under that enum. If you know your HTTP status codes, you would know that OK is represented by the status code 201. There are several other constants that can be used such as NOT_FOUND , BAD_REQUEST , UNAUTHORIZED , etc., which all have their represented status code. It is an enum that contains all HTTP status codes.

Output:

Screenshot by author

Another way this can be achieved is by using static methods of the ResponseEntity class.

We will create this alternative endpoint with /restresponsecodes2 .

@GetMapping("/restresponsecodes2")

public ResponseEntity responses2(){



return ResponseEntity.ok("created");

}

We are using the static method .ok . This is equivalent to all we did in the previous example, but we using the .ok method. This specified the same thing as putting HttpStatus.OK . All we now need is to state what message is to outputted in the response body. In this case, the message is “created” .

In these static ResponseEntity methods, there are some that require you to explicitly attach a body to the method.

Let's look at the endpoint below.

@GetMapping("/restresponsecodes3")

public ResponseEntity responses3(){



return ResponseEntity.badRequest().body("something went wrong");

}

For a bad request, you have to explicitly put a body to it with the message you want to return in JSON.

Output: