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 8904025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:01:07+00:00 2026-06-15T02:01:07+00:00

I am studying the Spring MVS Showcase downloadable from di STS dashboard. I am

  • 0

I am studying the Spring MVS Showcase downloadable from di STS dashboard. I am studying how Spring maps the request and I am having some problem to understand the following thing:

I have this form with a submit button:

        <li>
            <form id="byConsumes" class="readJsonForm" action="<c:url value="/mapping/consumes" />" method="post">
                <input id="byConsumesSubmit" type="submit" value="By consumes" />
            </form>
        </li>

When I click on the submit button a Jquery function that creates a JSON object is passed by the HTTP Post Request, this is the code of the JQuery function:

$("form.readJsonForm").submit(function() {          

    var form = $(this);                 // Variabile che si riferisce all'elemento nel DOM che ha scatenato l'evento click (il form) 
    var button = form.children(":first");       // Seleziona il bottone submit 

    var data = form.hasClass("invalid") ?       // OPERATORE CONDIZIONALE: il form ha classe "invalid" ? 
            "{ \"foo\": \"bar\" }" :            // SI: foo = bar 
            "{ \"foo\": \"bar\", \"fruit\": \"apple\" }";   // NO: foo= bar ; fruit = apple 


    /* AJAX CALL PARAMETER:
       type: Say to the servlet path, the request is a POST HTTP Request
       url: The address to which to send the call   
       data: the content of my data variable
       contentType: an object having JSON format
       dataType: the type of content returned by the server
    */
    $.ajax({ type: "POST", url: form.attr("action"), data: data, contentType: "application/json", dataType: "text", 
        success: function(text) { MvcUtil.showSuccessResponse(text, button); }, 
        error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});

    return false;
});

The JSON object that I have create and passed is represented by the data variable and contain the following key\value: { \”foo\”: \”bar\”, \”fruit\”: \”apple\” }

Something like:

foo: bar

fruit: apple

Now, in my controller I have the method that handles this request:

@RequestMapping(value="/mapping/consumes", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String byConsumes(@RequestBody JavaBean javaBean) {
    return "Mapped by path + method + consumable media type (javaBean '" + javaBean + "')";
}

So it is clear for me that this method handles HTTP Post Request towards “mapping/consumes” path (only POST Request) but I am not sure about the meaning of the following items:

  1. consumes=MediaType.APPLICATION_JSON_VALUE : what exactly does this mean? I think that it says to Spring that this method receives an object in JSON format, so it can be parsed in some way…but I am not sure about it and I have not found it in the documentation.

    What is consumes? a variable or something like an annotation? I am not understanding things because here it is a parameter of the @RequestMapping annotation but searching on Google shows it is used as a standalone annotation…

  2. In my byConsumes() method I have the following input parameter: @RequestBody JavaBean javaBean.
    Reading the Spring documentation I understand that: @RequestBody method parameter annotation indicates that using @RequestBody annotation a method parameter should be bound to the value of the HTTP request body.

    So in practice this thing mean that I have my JSON object inside my HTTP Request body field and using this annotation I am converting it in an object named javaBean having class JavaBean?

    If my affirmation is true…what kind of object is a JavaBean type object? an object that contains only a number of variables and the corresponding getter and setter methods? (in the previus case an object that contain only two variable: the first one named foo and having value “bar”, the second one having name fruit and having value “apple”)

is it right?

Thank you very much
Andrea

  • 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-15T02:01:08+00:00Added an answer on June 15, 2026 at 2:01 am

    Your interpretation of what this code is doing is essentially correct.

    ‘Consumes’ is a parameter of the annotation @RequestMapping which is used to indicate the HTTP content type that the method handles. In this case it is indicating to Spring that the method takes JSON as input. You can read more about this in the Spring documentation here. You could similarly indicate that a method ‘consumes’ XML.

    As for @RequestBody annotation, this is used to indicate the annotated method parameter should be composed from the body of the HTTP request, the documentation for it is here. Spring will usually do this by taking the name of the HTTP parameter in the request and setting the value in the Java bean using a setter method.

    In your example, Spring will inspect the annotations of the method and determine that the request body should be mapped to an object of type JavaBean. It will create an instance of that class automatically, and (via a Message Converter) will populate the JavaBean instance. Spring will use reflection to do all of this, but the code it executes would essentially do this:

    JavaBean parameter = new JavaBean();
    parameter.setFoo("bar");
    parameter.setFruit("apple");
    

    You could swap out the JavaBean type for any other Java class that declares getters/setters for fields foo and fruit.

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

Sidebar

Related Questions

in this period I am studying the Spring MVC showcase example dowlodable from STS
In this period I am studing the Spring MVC showcase example (downloadable from STS
I am studing the Spring MVC showwcase dowloadable from the STS dashboard. Now I
Studying some code from a codeigniter tut, the following preg_match pattern has me baffled:
While studying for a Functional Programming exam, I came across the following questions from
While studying for a Functional Programming exam, I came across the following question from
I've got my first impression of Spring WebFlow 2.1 from studying the reference app
In this period I am studying the Sping MVC showcase example dowlodable form STS
I have being studying (newbie) .NET and I got some doubts. Reading from a
studying some sample code from an iOS programming course (cs193p fall2010) i came across

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.