Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda