bad request when sending number to Spring controller

Asked

Viewed 38 times

1

I have a form that sends some data to a controller, when I send only text data works, but when I send a given type number ( and that in the controller I hope to receive as parameter a Double) gives bad request error. If a field is sent empty, I deal with jquery to identify the empty fields in a service (if the field is not filled in jquery fills with 'null' and then my service understands that it has not been filled in).

Form

<form action="/pecas/teste" id="pesquisa">
                Descrição: 
                <input type="text" name="descricao" id="desc">
                Categoria:
                <input type="text" name="categoria" id="cat">
                Tamanho:
                <input type="text" name="categoria" id="tam">
                Cor:
                <input type="text" name="cor" id="cor">
                Preço:
                <input type="number" name="preco" id="preco">

                <input type="submit">
            </form>

Jquery

$("#pesquisa").submit(function(){
            if($("#desc").val() == '')
                $("#desc").val('null');

            if($("#cat").val() == '')
                $("#cat").val('null');

            if($("#tam").val() == '')
                $("#tam").val('null');

            if($("#cor").val() == '')
                $("#cor").val('null');

            //como não sei o que está dando errado
            //não tô fazendo o tratamento para preco

        });

Controller

@RequestMapping(path="/teste")
public ModelAndView mostrarPecas(@RequestParam String descricao, @RequestParam String categoria, @RequestParam String cor,
        @RequestParam String tamanho, @RequestParam Double preco) {

    ModelAndView mv = new ModelAndView("verpecas");

    List<Peca> pecas = service.getPecas(descricao, categoria, tamanho, cor, preco);

    mv.addObject("pecas", pecas);

    return mv;
}
  • Hello! Let us know which Exception Spring fires when this occurs. I also recommend using BigDecimal instead of Double for monetary values, to avoid accuracy problems in the future.

  • @Dherik, vlw by retornor! I was able to solve, actually it was not the price that was giving bad request, but the size, because his "name" was wrong. What kind of trouble can I have with double?

  • 1

    explanation is a little extensive, but you can check it here (in English): https://stackoverflow.com/a/3413493/2387977

No answers

Browser other questions tagged

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