Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8172991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:58:34+00:00 2026-06-06T21:58:34+00:00

Can someone explain the @RequestBody and @ResponseBody annotations in Spring 3? What are they

  • 0

Can someone explain the @RequestBody and @ResponseBody annotations in Spring 3? What are they for? Any examples would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:58:39+00:00Added an answer on June 6, 2026 at 9:58 pm

    There is a whole Section in the docs called 16.3.3.4 Mapping the request body with the @RequestBody annotation. And one called 16.3.3.5 Mapping the response body with the @ResponseBody annotation. I suggest you consult those sections. Also relevant: @RequestBody javadocs, @ResponseBody javadocs

    Usage examples would be something like this:

    Using a JavaScript-library like JQuery, you would post a JSON-Object like this:

    { "firstName" : "Elmer", "lastName" : "Fudd" }
    

    Your controller method would look like this:

    // controller
    @ResponseBody @RequestMapping("/description")
    public Description getDescription(@RequestBody UserStats stats){
        return new Description(stats.getFirstName() + " " + stats.getLastname() + " hates wacky wabbits");
    }
    
    // domain / value objects
    public class UserStats{
        private String firstName;
        private String lastName;
        // + getters, setters
    }
    public class Description{
        private String description;
        // + getters, setters, constructor
    }
    

    Now if you have Jackson on your classpath (and have an <mvc:annotation-driven> setup), Spring would convert the incoming JSON to a UserStats object from the post body (because you added the @RequestBody annotation) and it would serialize the returned object to JSON (because you added the @ResponseBody annotation). So the Browser / Client would see this JSON result:

    { "description" : "Elmer Fudd hates wacky wabbits" }
    

    See this previous answer of mine for a complete working example: https://stackoverflow.com/a/5908632/342852

    Note: RequestBody / ResponseBody is of course not limited to JSON, both can handle multiple formats, including plain text and XML, but JSON is probably the most used format.


    Update

    Ever since Spring 4.x, you usually won’t use @ResponseBody on method level, but rather @RestController on class level, with the same effect.

    Here is a quote from the official Spring MVC documentation:

    @RestController is a composed annotation that is itself meta-annotated
    with @Controller and @ResponseBody to indicate a controller whose
    every method inherits the type-level @ResponseBody annotation and,
    therefore, writes directly to the response body versus view resolution
    and rendering with an HTML template.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain which indexed search would be faster, listing from fastest, to slowest
Can someone explain the mechanics of a jump table and why is would be
Can someone explain this simple, yet deceiving, anomaly? There are two models, where B
Can someone explain me why this works: select val1, val2, count(case when table2.someID in
Can someone explain why, in this situation, scandir is getting my directory, but the
Can someone explain step by step how this query is processed? SELECT F.ID, F.FirstName,
Can someone explain why it's best use is for prototyping? Should it not be
Can someone explain how the View and ViewModel are connected? I can't find anywhere
Can someone explain how the organisation of classes in Pharo works in different versions
Can someone explain the threading model in windows metro? I really get confusion about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.