Send List + Parameter to Controller

Asked

Viewed 689 times

0

I am trying to send a list and parameter to my controller. My controller is as follows:

@RequestMapping("/lista-receitas")
@ResponseBody
public String recebeLista(@RequestBody List<Receita> receitas, @RequestParam("nome") String nome){
    System.out.println("Receita: "+ nome);
    for(Receita r: receitas){
        System.out.println("ID: "+r.getId() +" Nome: "+ r.getNome());
    }

    return "Sucesso";
}

In my method I want to receive a list and a name.

My Javascritp is as follows:

function lista(){
    var lista=[];
    var Receita = new Object();
    var receitas = $('.receita');

    $(receitas).each(function(i){
        Receita = {
            id: $('#'+receitas[i].id).find("#id").text().trim(),
            nome: $('#'+receitas[i].id).find("#nome").text().trim()
         }
         lista.push(Receita);
   })
   return lista;
}


function enviarLista(){
    var receita = lista();
    var nome = "adm";
    jQuery.ajax({
        type:  'POST',
        //contentType: "application/json",
        url:   'lista-receitas',       
       data:{
            receita:JSON.stringify(receita),
            nome: nome
       },
      //dataType: 'json',
      success: function(data){          
        alert(data);
      } 
  }); 
}

If you send only the list and remove the parameter name my controller works normally.

2 answers

0

Try changing the order of the Controller parameters

@RequestMapping("/lista-receitas")
@ResponseBody
public String recebeLista(@RequestParam("nome") String nome, @RequestBody List<Receita> receitas){
    System.out.println("Receita: "+ nome);
    for(Receita r: receitas){
        System.out.println("ID: "+r.getId() +" Nome: "+ r.getNome());
    }

    return "Sucesso";
}
  • I did the inversion, but is giving error 404 saying the parameter name is not being passed.

  • Try modifying the Ajax url to: 'list-recipes? name='+,

  • Now yes it worked huh, thank you very much!! You the pqe of not working send the list and the parameter together?

  • 1

    @Danielmoura, in theory, this sending of information is, semantically, a mess. When you send the parameter via POST traditional, it will go inside the RequestBody, then you end up mixing things. The renanzin solution was to send this parameter by URL (traditionally, parameter GET; also known as QueryParam). Particularly I find it messy to use so I would prefer to create a parameterization through the PATH, not of QueryParam...

  • 1

    @Danielmoura, I use this parameterization in that matter; something like this: @RequestMapping(path = "/{relat}.pdf", ...)&#xA; public void requisicaoPdf(@PathVariable("relat") String relat ...); the argument is treated as part of the PATH which is between keys, and the parameter is indicated by the annotation @PathVariable

  • I’ll test that other way.

Show 1 more comment

0

If you are using spring you can use these syntax

@RequestMapping(value = "lista-receitas",method = RequestMethod.POST,consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})

or

@PostMapping("lista-receitas")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.