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.
I did the inversion, but is giving error 404 saying the parameter name is not being passed.
– DSCM
Try modifying the Ajax url to: 'list-recipes? name='+,
– renanvm
Now yes it worked huh, thank you very much!! You the pqe of not working send the list and the parameter together?
– DSCM
@Danielmoura, in theory, this sending of information is, semantically, a mess. When you send the parameter via
POST
traditional, it will go inside theRequestBody
, then you end up mixing things. The renanzin solution was to send this parameter by URL (traditionally, parameterGET
; also known asQueryParam
). Particularly I find it messy to use so I would prefer to create a parameterization through thePATH
, not ofQueryParam
...– Jefferson Quesado
@Danielmoura, I use this parameterization in that matter; something like this:
@RequestMapping(path = "/{relat}.pdf", ...)
 public void requisicaoPdf(@PathVariable("relat") String relat ...)
; the argument is treated as part of thePATH
which is between keys, and the parameter is indicated by the annotation@PathVariable
– Jefferson Quesado
I’ll test that other way.
– DSCM