Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
Posting dynamic JSON-Object to Spring RESTful Web Service

I am trying to post dynamic JSON-Object like

{  
   "name":[  
      {  
         "key":"myKey1",
         "value":"myValue1"
      },
      {  
         "key":"myKey2",
         "value":myValue2
      },
      ....
   ]
}

to Spring RESTful Web Service but I want to get JSON-Object as JSON-Object not as String my code is:

    @RequestMapping(path ="/hi", method = RequestMethod.POST, consumes = "application/json")     
public Greeting hi(@RequestBody String jobject) {  
return new Greeting (100,jobject); 
}
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Since you need key-value pairs , you can do something like below: You can define a POJO which contains a map.. Something like below:

@RequestMapping(value = "/get/{searchId}", method = RequestMethod.POST)
public String search(
@PathVariable("searchId") Long searchId,
@RequestParam SearchRequest searchRequest) {
 System.out.println(searchRequest.getParams.size());
 return "";
}

public class SearchRequest {   
private Map<String, String> params;
}

Request Object:

"params":{
     "birthDate": "25.01.2011",
    "lang":"en"       
 }
about 4 years ago · Santiago Trujillo Report

0

you can get it as String and convert it to json with JSON.parse or somthing like that !! or you can use

@RequestMapping(path ="/test", method = RequestMethod.POST, consumes = "application/json")     
public myMethodehi(@RequestBody Pojo pojo) {  

}
about 4 years ago · Santiago Trujillo Report

0

Create a pojo class correspoding to your json.

public class MyPojo
{
    private Name[] name;

    public Name[] getName ()
    {
        return name;
    }

    public void setName (Name[] name)
    {
        this.name = name;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [name = "+name+"]";
    }
}

public class Name
{
    private String value;

    private String key;

    public String getValue ()
    {
        return value;
    }

    public void setValue (String value)
    {
        this.value = value;
    }

    public String getKey ()
    {
        return key;
    }

    public void setKey (String key)
    {
        this.key = key;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [value = "+value+", key = "+key+"]";
    }
}

You can use some online json to pojo converter. I use http://www.jsonschema2pojo.org/ Just paste json there and click convert.

Now instead of string specify your POJO class, spring will do the conversion for you

@RequestMapping(path ="/hi", method = RequestMethod.POST, consumes = "application/json")     
public Greeting hi(@RequestBody MyPojo myPojo) {  

    // return new Greeting (100,jobject); 
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!