Retrieve BigDecimal Value from @PathParam

1

I have the following call

http://localhost:8080/app-teste/produto/1234/76.60

and I want to recover like this:

@Get("/{produtoDto.codBarras}/{preco}")
public void produtoComPreco(ProdutoDto produtoDto, @PathParam("preco") 
BigDecimal preco) {
System.out.println(preco); // imprime 7660

...

however, the price point = 76.60 goes out and gets only 7660

Can you keep this point?

I'm using VRAPTOR.

    
asked by anonymous 04.01.2018 / 15:25

1 answer

0

Converting to BigDecimal of VRaptor is expecting a comma to decimal places because of your server's Locale (which is probably pt-BR ). If you make your call like this: http://localhost:8080/app-teste/produto/1234/76,60 it should convert correctly, however I do not recommend leaving it like this.

What you can do is to get a String and convert to BigDecimal normally

@Get("/{produtoDto.codBarras}/{preco}")
public void produtoComPreco(ProdutoDto produtoDto, @PathParam("preco") 
String preco) {
    System.out.println(new BigDecimal(preco));
}

You can try to make Locale adjustment, but it must be set to pt-BR , according to your server, and changing to another locale can influence your entire application.

Another solution would be to overwrite the BigDecimal converter, if you find it necessary.

Here is a link from a person who experienced this problem: link

And a link to a related issue: link

    
04.01.2018 / 17:33