Retrieve Bigdecimal Value from @Pathparam

Asked

Viewed 84 times

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 adds up and is only 7660

You can keep that point?

I am using VRAPTOR.

  • Have you tried passing the value with a comma? Maybe it’s some configuration of Locale.

1 answer

0

Convert it to BigDecimal Vraptor is expecting a comma to decimals 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 must convert correctly, but I do not recommend you leave so.

What you can do is receive one String and make the conversion 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 adjusting Locale but it must be set to pt-BR, according to your server, and changing to another locale may influence your entire application.

Another solution would be to overwrite the Bigdecimal Converter, if you find it necessary.

Follow a link from a person who has been through this problem: http://www.guj.com.br/t/resolvido-vraptor-3-4-1-problema-com-converter/298683/2

And a link to a related link: https://github.com/caelum/vraptor4/issues/504

  • Show. Thank you! I will receive as String and convert. Thank you very much, Explanation too good!!!

Browser other questions tagged

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