user
user

Reputation: 427

How to send Json array from ajax to spring Controller?

I have an html table like below from which I convert the data to a json form and send via ajax to a spring controller. In the spring controller I used @RequestParam Map<String, String> to get the values, but I got the whole json string as the only key. Using modelAttribute I can achieve this, but I have different scenarios so I cannot use the model class so I also want these column headers with their values.

<table>
<thead>
<tr>
    <th>Column 1</th>
    <th>Column 2</th>
    <th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
    <td>A1</td>
    <td>A2</td>
    <td></td>
</tr>
<tr>
    <td>B1</td>
    <td>B2</td>
    <td>B3</td>
</tr>
<tr>
    <td>C1</td>
    <td></td>
    <td>C3</td>
</tr>
</tbody>

I convert the html table data to json and send via ajax. -

  [
    {
    "Column 1": "A1",
    "Column 2": "A2",
    "Column 3": ""
    },
   {
    "Column 1": "B1",
    "Column 2": "B2",
    "Column 3": "B3"
   },
   {
    "Column 1": "C1",
    "Column 2": "",
    "Column 3": "C3"
   }
  ]

ajax code -

$.ajax({
        type: "POST",
        //contentType : 'application/json; charset=utf-8',
        //dataType : 'json',
        url: "/gDirecotry/" + id,
        data: JSON.stringify(rows),
        success: function (result) {
            console.log(result);
        }
    });

Spring controller code -

  @RequestMapping(value = "/gDirecotry/{id}", method = RequestMethod.POST)
  public @ResponseBody ModelAndView 
      getSearchUserProfiles(@PathVariable("id") String id,
                                 @RequestParam Map<String, String> 
   attributeMap) throws IOException {

return new ModelAndView("redirect:/home");
  }

I want to map the json data to map<string,string>, how could I acheive this?

OUTPUT : - I got this whole string as the only key without value.

[{"Column 1":"A1","Column 2":"A2","Column 3":""},{"Column 1":"B1","Column 
2":"B2","Column 3":"B3"},{"Column 1":"C1","Column 2":"","Column 3":"C3"}]

Upvotes: 3

Views: 1462

Answers (2)

Bruno Tonini
Bruno Tonini

Reputation: 39

example for a array of objects.

Controller:

@RequestMapping(value = "alterarSolicitacoesPerfil.do", method = RequestMethod.POST)
@ResponseBody
public Boolean alterarSolicitacoes(@RequestBody SolicitacaoPerfilAcessoRespostaVO[] solicitacoes) throws Exception  {
    List<SolicitacaoPerfilAcessoRespostaVO> listaSolicitacoes = Arrays.asList(solicitacoes);
    controllerService.alterarSolicitacoesPerfilAcesso(listaSolicitacoes);
    return true;
}

Ajax:

alterarSolicitacoes = function (){
var dataArray = new Array();
    
for(var i = 0; i < 10; i++){
 var data = {};
 data.id = i
 data.desc = "teste" 
 dataArray.push(data)
}

    $.ajax({
            type: "POST",
            dataType: 'json',
            contentType: 'application/json', 
            data: JSON.stringify(dataArray),
            url: "./alterarSolicitacoesPerfil.do",
            success: function (dataR) {
               return dataR
            },
            error: function(errorMessage) {}
          });
};

Upvotes: -1

Michał Ziober
Michał Ziober

Reputation: 38635

Change:

@RequestParam Map<String, String> attributeMap

To

@RequestBody List<Map<String, String>> attributeMap

Your JSON payload is an array of objects.

Upvotes: 4

Related Questions