Giter Club home page Giter Club logo

spring-demo's Introduction

Spring-Demo

#In this I've Created a Demo On Basic Spring Application

#How to use @RequestBody and @ResponseBody Annotations in Spring

As the name suggest @RequestBody annotation is used to parse the incoming HTTP request while @ResponseBody annotation is used to convert your object into HTTP response in the form client is expecting like JSON, XML, or simply text. This is the basic, now let's look at these two annotations in detail.

#1. @RequestBody annotation

The @RequestBody annotation links the HTTPRequest body to the domain object, in layman's words. Using HTTP Message Converters, the Spring framework automatically de-serializes incoming HTTPRequests and converts them to Java objects.

To resolve the method argument based on the content type of the request, we send the body of the request via a HttpMessageConverter. If the preceding definitions appear confusing, the @RequestBody annotation allows us to get the request's content and transform it into a Java Object automatically.

Let's look at the procedure of registering a cricket player as an example.

For registration, API requires cricketer data. The system will transmit cricketer data via HTTPRequest body if the cricketer fills out the whole registration form.

So, let's explain in terms of code

Below is our data model.

image

Now, let's create a controller and explain where exactly @RequestBody annotation is used.

image

If a suitable Java type is given, Spring will automatically deserialize the JSON and convert it into a Java object much like we manually do using Jackson or Gson as explained here. As seen in the code above, the Registration object is annotated with @RequestBody which means the PostRequest has a body that contains the JSON format of the Registration object which is captured as a parameter by method register via our @RequestBody annotation.

Java will automatically deserialize the incoming JSON into a Registration object if the JSON is valid and as per the Object naming conventions.

This all is done by our annotation.

If we test our application via postman or any other client and pass the below data as body,

image

#############################################################################################

@ModelAttribute is used for binding data from request param (in key value pairs),

but @RequestBody is used for binding data from whole body of the request like POST,PUT.. request types which contains other format like json, xml.

##############################################################################################

Spring automatically converts receiving JSON data in the HTTPRequestBody to the Registration java object, as seen in this example.

#2. @ResponseBody annotation in Spring MVC

To put it another way, @ResponseBody instructs the Spring framework to serialize a return object into JSON or XML and return it as part of the HTTPResponse.

When interacting with the REST API in Spring 4.x, we should use @RestController on a class level rather than @ResponseBody on a method level.

@RestController is a constructed annotation with @Controller and @ResponseBody that is itself a meta-annotation.

In the previous example, our register function returns a Cricketer object, which we want to deliver in the HTTPResponse as JSON.

We may still specify the content type that our function returns when we use the @ResponseBody annotation.

The generated property of the @RequestMapping may be used for this. Note that annotations like @PostMapping, @GetMapping, etc. define aliases for that parameter.

As seen in our controller below, we have @RestController annotation so we don't need @ResponseBody anymore. The @RestController will automatically add @ResponseBody when needed.

image

But, if @RestController for any reason could not be annotated at the class level, we can anyways use @ResponseBody as shown below.

image

As shown above, we dictate that we are sending back the Cricketer object as a response. With @ResponseBody annotation, java automatically serializes it into JSON and sends it back.

image

That's all about how to use Spring @RequestBody and @ResponseBody annotations in Java and Spring MVC. These annotations are crucial when using Spring and

Spring Boot to develop our REST API. You can use them for parsing request data into your domain object as well as converting your domain object to HTTP response in

the format your client wants like JSON, XML or TEXT. If you use @RestContorller which you should then you don't need to explicitly use @RequestBody and

@ResponseBody, its already taken care.

spring-demo's People

Contributors

iamkishor07 avatar ajay2330 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.