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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:20:34+00:00 2026-06-13T01:20:34+00:00

Is it necessary to wrap in a backing object? I want to do this:

  • 0

Is it necessary to wrap in a backing object? I want to do this:

@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody String str1, @RequestBody String str2) {}

And use a JSON like this:

{
    "str1": "test one",
    "str2": "two test"
}

But instead I have to use:

@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody Holder holder) {}

And then use this JSON:

{
    "holder": {
        "str1": "test one",
        "str2": "two test"
    }
}

Is that correct? My other option would be to change the RequestMethod to GET and use @RequestParam in query string or use @PathVariable with either RequestMethod.

  • 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-13T01:20:35+00:00Added an answer on June 13, 2026 at 1:20 am

    You are correct, @RequestBody annotated parameter is expected to hold the entire body of the request and bind to one object, so you essentially will have to go with your options.

    If you absolutely want your approach, there is a custom implementation that you can do though:

    Say this is your json:

    {
        "str1": "test one",
        "str2": "two test"
    }
    

    and you want to bind it to the two params here:

    @RequestMapping(value = "/Test", method = RequestMethod.POST)
    public boolean getTest(String str1, String str2)
    

    First define a custom annotation, say @JsonArg, with the JSON path like path to the information that you want:

    public boolean getTest(@JsonArg("/str1") String str1, @JsonArg("/str2") String str2)
    

    Now write a Custom HandlerMethodArgumentResolver which uses the JsonPath defined above to resolve the actual argument:

    import java.io.IOException;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.commons.io.IOUtils;
    import org.springframework.core.MethodParameter;
    import org.springframework.http.server.ServletServerHttpRequest;
    import org.springframework.web.bind.support.WebDataBinderFactory;
    import org.springframework.web.context.request.NativeWebRequest;
    import org.springframework.web.method.support.HandlerMethodArgumentResolver;
    import org.springframework.web.method.support.ModelAndViewContainer;
    
    import com.jayway.jsonpath.JsonPath;
    
    public class JsonPathArgumentResolver implements HandlerMethodArgumentResolver{
    
        private static final String JSONBODYATTRIBUTE = "JSON_REQUEST_BODY";
        @Override
        public boolean supportsParameter(MethodParameter parameter) {
            return parameter.hasParameterAnnotation(JsonArg.class);
        }
    
        @Override
        public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
            String body = getRequestBody(webRequest);
            String val = JsonPath.read(body, parameter.getMethodAnnotation(JsonArg.class).value());
            return val;
        }
    
        private String getRequestBody(NativeWebRequest webRequest){
            HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
            String jsonBody = (String) servletRequest.getAttribute(JSONBODYATTRIBUTE);
            if (jsonBody==null){
                try {
                    String body = IOUtils.toString(servletRequest.getInputStream());
                    servletRequest.setAttribute(JSONBODYATTRIBUTE, body);
                    return body;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return "";
    
        }
    }
    

    Now just register this with Spring MVC. A bit involved, but this should work cleanly.

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

Sidebar

Related Questions

I'm struggle extracting necessary data from this json stored inside php variable. Not yet
struggle extracting necessary data from this complex json stored inside php variable. Via php
Is it necessary to check for nullptr after allocating an object using gcnew?
Is it really necessary to wrap structs/classes with Boost.Fusion in order to use them
I want to wrap my SQL deployment script in a transaction (containing a bunch
I want to decrease a value by one and if it reaches zero, set
In HTML, should block level elements always wrap <a> tags? What if it's necessary
I am trying to wrap my head around this one this morning. I am
not necessary an UML diagram, just a simple way to graphically document the relations
Is it necessary that the assignment operator ( = ) always copies the rvalue

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.