DEV Community

Cover image for Spring - @RequestBody and @ResponseBody
Yiğit Erkal
Yiğit Erkal

Posted on

3 3

Spring - @RequestBody and @ResponseBody

@RequestBody

Annotation indicating a method parameter should be bound to the body of the HTTP request. With the @RequestBody annotation, POST or PUT requests are handled. It is generally used to convert a request into an object in JSON or XML format.

RequestBody Spring Annotation

For example:

@RequestMapping(value = "/isConverted", method = RequestMethod.POST)
@ResponseBody
public String isConvertedFromJson(@RequestBody User user) {
    return user.getUserName();
}
Enter fullscreen mode Exit fullscreen mode

@ResponseBody

It can be put on a method and indicates that the return type should be written straight to the HTTP response body. Not placed in a Modal or View name. With the @ResponseBody annotation, we can return values ​​of multiple types such as String, application/json or application/xml.

ResponseBody Spring Annotation

For example:

@RequestMapping(value = "/produceString", method = RequestMethod.GET)
@ResponseBody
public String produceString() {
     return "Hello World";
}
Enter fullscreen mode Exit fullscreen mode

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay