CBD Lowers Blood Pressure

According to the abstract from the, “Journal of Clinical Investigation” Cannabidiol (CBD) is a nonpsychoactive phytocannabinoid used in multiple sclerosis and intractable epilepsies. Preclinical…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Simple Spring Boot RESTful Service

We will create a simple Spring Boot app that will let us view all our comments, add a new comment, or edit an existing comment.

I have changed the group as com.pal.vidisha.springreact and the artifact as comments. The rest of the default values remains unchanged. Now, let us add the dependencies. Click on Add Dependencies button and add the below dependencies :

Click on generate, unzip the downloaded file and open the project in your IDE.

The generated project has a class called CommentsApplication.java. This is the main class for the Spring Boot application and is annotated with @SpringBootApplication.

Next we will create our controller and DTO classes.

Create a package called controller inside src/main/java/com/pal/vidisha/springreact/comments folder and create a Java class called CommentsController.java

Create another package called dto and create a Java class called Comment.java

Comment.java

The Comment.java class is a DTO class. It is annotated with @Data, @AllArgsConstructor, @NoArgsConstructor which automatically creates getters, setters, an all argument constructor and no argument constructor.

CommentsController.java

We annotate this class with @RestController. This annotation makes it possible to make RESTful API calls to methods within the class.

The @ResquestMapping annotation appends the path /comments. Any API calls to the path /comments will be directed to this controller class.

We have a method called getComment that gets a particular comment based on the commentId. This method accepts a GET API request, annotated by @GetMapping.

The method parameter commentId is annotated with @PathVariable and accepts the commentId from the URI path.

To add a comment, we send in a POST API request, with the comment details in the body of the request. To read the request body, we annotate a method parameter with @RequestBody and DTO class that corresponds to the comment.

The @PostMapping annotation has two attributes consumes and produces attributes. This is optional, and when defined determines the type of request and response that is accepted in the RESTful request and response.

Here, we have allowed only JSON and XML formats.

The API response is a ResponseEntity of type Comment and the HTTP status is CREATED.

Similar to adding a comment, we can update a comment by passing in the comment as a parameter with the @RequestBody annotation. We also have the commentId in the path of the PUT request annotated with @PutMapping. This variable is used to determine which comment is being updated.

Run the application with mvn spring-boot:run command in the terminal or running it directly from the IDE.

The application starts on built in tomcat server on port 8080.

We will test the application using Postman.

Add a new comment

We add request headers :

Content-Type — “application/json”

Accept — “application/json

The request body is as below:

We get a response with status code of 201 as well as the comment as a response.

Retrieve a comment

We get the response as a string.

Update a comment

As the request body, we can have the below JSON as an example.

Thank you for reading!

Next, we will connect to H2 database from our Spring Boot application and save comments to the database. The details can be found here.

Add a comment

Related posts:

Are School Sports Still Fun?

Have you ever been interested in playing a sport here at Eastside, but couldn’t motivate yourself to participate? Have you wanted to play a sport that wasn’t offered at Eastside? Many students lack…