Roberta  Ward

Roberta Ward

1602550923

Get HTTP POST Body in Spring Boot with @RequestBody

Introduction

With Spring, we map requests to request handlers via the @RequestMapping annotation. Spring Boot introduced us to derived types of this annotation - @GetMapping@PostMapping@DeleteMapping, etc.

These requests contain different types of information and data - and depending on what our endpoint does with the request, we may want to retrieve the body of a POST request either for logging or further processing.

The @RequestBody annotation allows us to retrieve the request’s body. We can then return it as a String or deserialize it into a Plain Old Java Object (POJO).

Spring has built-in mechanisms for deserializing JSON and XML objects into POJOs, which makes this task a lot easier as well.

In this article, we’ll get the body of a POST HTTP request and pack it into a String, as well as deserialize it into a POJO.

Getting HTTP POST Body as a String

Let’s start out with a @Controller class to handle an incoming request:

@ResponseBody
@Controller
@RequestMapping("/response")
public class HomeController {}

What’s worth noting here is that we’ve added the @ResponseBody annotation to the @Controller. This way, it won’t render a template as the response body, but rather, will return a response body. Specifically, we’ll pack it in a String and return that.

Spring Boot introduced us to another derived type - @RestController which is just a combination of the previous two annotations. Let’s use that instead for brevity:

@RestController
@RequestMapping("/response")
public class HomeController {}

Now, let’s go ahead and define a request handler. Since we’re handling a POST request, we’ll use the @PostMapping annotation for it:

@RestController
@RequestMapping("/response")
public class HomeController {

    @PostMapping("/postbody")
    public String postBody(@RequestBody String fullName) {
        return "Hello " + fullName;
    }
}

To retrieve the body of the POST request sent to the handler, we’ll use the @RequestBody annotation, and assign its value to a String. This takes the body of the request and neatly packs it into our fullName String. We’ve then returned this name back, with a greeting message.

#java #spring #spring boot #http

What is GEEK

Buddha Community

Get HTTP POST Body in Spring Boot with @RequestBody
Roberta  Ward

Roberta Ward

1602550923

Get HTTP POST Body in Spring Boot with @RequestBody

Introduction

With Spring, we map requests to request handlers via the @RequestMapping annotation. Spring Boot introduced us to derived types of this annotation - @GetMapping@PostMapping@DeleteMapping, etc.

These requests contain different types of information and data - and depending on what our endpoint does with the request, we may want to retrieve the body of a POST request either for logging or further processing.

The @RequestBody annotation allows us to retrieve the request’s body. We can then return it as a String or deserialize it into a Plain Old Java Object (POJO).

Spring has built-in mechanisms for deserializing JSON and XML objects into POJOs, which makes this task a lot easier as well.

In this article, we’ll get the body of a POST HTTP request and pack it into a String, as well as deserialize it into a POJO.

Getting HTTP POST Body as a String

Let’s start out with a @Controller class to handle an incoming request:

@ResponseBody
@Controller
@RequestMapping("/response")
public class HomeController {}

What’s worth noting here is that we’ve added the @ResponseBody annotation to the @Controller. This way, it won’t render a template as the response body, but rather, will return a response body. Specifically, we’ll pack it in a String and return that.

Spring Boot introduced us to another derived type - @RestController which is just a combination of the previous two annotations. Let’s use that instead for brevity:

@RestController
@RequestMapping("/response")
public class HomeController {}

Now, let’s go ahead and define a request handler. Since we’re handling a POST request, we’ll use the @PostMapping annotation for it:

@RestController
@RequestMapping("/response")
public class HomeController {

    @PostMapping("/postbody")
    public String postBody(@RequestBody String fullName) {
        return "Hello " + fullName;
    }
}

To retrieve the body of the POST request sent to the handler, we’ll use the @RequestBody annotation, and assign its value to a String. This takes the body of the request and neatly packs it into our fullName String. We’ve then returned this name back, with a greeting message.

#java #spring #spring boot #http

Were  Joyce

Were Joyce

1620751200

How to Configure the Interceptor With Spring Boot Application

In the video in this article, we take a closer look at how to configure the interceptor with the Spring Boot application! Let’s take a look!

#spring boot #spring boot tutorial #interceptor #interceptors #spring boot interceptor #spring boot tutorial for beginners

Were  Joyce

Were Joyce

1620720872

Spring vs Spring BooDifference Between Spring and Spring Boot

As an extension of the Spring Framework, Spring Boot is widely used to make development on Spring faster, more efficient and convenient. In this article, we will look at some of the parameters were using Spring Boot can drastically reduce the time and effort required in application development.

What is Spring?

Spring Boot

Difference between Spring and Spring Boot

Advantages of Spring Boot over Spring

Conclusion

#full stack development #spring #spring and spring boot #spring boot

Sigrid  Farrell

Sigrid Farrell

1622601303

How to Configure log4j2 In a Spring Boot Application? | Spring Boot Logging [Video]

Configuring log4j2 is really quick and simple; this tutorial video explains the entire process in only 5 minutes, while you wait for your coffee to brew.

In the video below, we take a closer look at the How to configure log4j2 in the Spring boot application using log4j2.xml? | Spring Boot logging. Let’s get started!

#java #spring boot #video #log4j #spring boot tutorial #spring boot tutorial for beginners

Were  Joyce

Were Joyce

1620844680

Spring Boot 2 With JSP View

In this tutorial, I am going to show you how easy it is to create a web application with Spring Boot 2, along with the embedded Tomcat + JSP template and JSP views.

What You’ll Need

1. Project Structure

You can get the blueprint from the Spring Initializer page.

2. Project Dependencies

3. Configuration

4. Controller and View Template

5. Run With Maven

#java #tutorial #spring boot #jsp #spring boot 2 #spring boot tutorial #jsp view