receber BigDecimal

66 views
Skip to first unread message

Fabio Ebner

unread,
Jan 31, 2015, 3:56:57 PM1/31/15
to Vraptor
Estou tendo esse erro ao receber um BigDecimal:


anamerNameProvider] Found parameter names with paranamer for public void br.com.lumera.integrabrasil.controllers.ComunicadoController.informarCustasComunicado(java.math.BigDecimal,java.lang.String) as [vlCustas, protocolo]
18:55:25,056 DEBUG [IogiParametersProvider] getParametersFor() called with parameters Parameters(Parameter(protocolo -> CP2301000000412615), Parameter(vlCustas -> 2)) and targets [Target(name=vlCustas, type=class java.math.BigDecimal), Target(name=protocolo, type=class java.lang.String)].
18:55:25,056 DEBUG [DefaultConverters   ] Unable to find a converter for class java.math.BigDecimal. Returning NullConverter.
Jan 31, 2015 6:55:25 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [default] in context with path [/integranovo] threw exception
br.com.caelum.vraptor.http.InvalidParameterException: Exception when trying to instantiate Target(name=vlCustas, type=class java.math.BigDecimal)
at br.com.caelum.vraptor.http.iogi.VRaptorInstantiator.handleException(VRaptorInstantiator.java:128)
at br.com.caelum.vraptor.http.iogi.VRaptorInstantiator.handleException(VRaptorInstantiator.java:130)
at br.com.caelum.vraptor.http.iogi.VRaptorInstantiator.instantiate(VRaptorInstantiator.java:120)
at br.com.caelum.vraptor.http.iogi.VRaptorInstantiator.instantiate(VRaptorInstantiator.java:112)
at br.com.caelum.vraptor.http.iogi.VRaptorInstantiator$Proxy$_$$_WeldClientProxy.instantiate(Unknown Source)
at br.com.caelum.vraptor.http.iogi.IogiParametersProvider.instantiateOrAddError(IogiParametersProvider.java:87)
at br.com.caelum.vraptor.http.iogi.IogiParametersProvider.instantiateParameters(IogiParametersProvider.java:80)
at br.com.caelum.vraptor.http.iogi.IogiParametersProvider.getParametersFor(IogiParametersProvider.java:72)
at br.com.caelum.vraptor.http.iogi.IogiParametersProvider$Proxy$_$$_WeldClientProxy.getParametersFor(Unknown Source)
at br.com.caelum.vraptor.observer.ParametersInstantiator.getParametersForCurrentMethod(ParametersInstantiator.java:149)
at br.com.caelum.vraptor.observer.ParametersInstantiator.instantiate(ParametersInstantiator.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93)
at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266)
at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253)
at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232)
at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169)
at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165)
at org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119)
at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112)
at org.jboss.weld.event.EventImpl.fire(EventImpl.java:83)
at br.com.caelum.vraptor.core.DefaultInterceptorStack.start(DefaultInterceptorStack.java:90)
at br.com.caelum.vraptor.core.DefaultInterceptorStack$Proxy$_$$_WeldClientProxy.start(Unknown Source)
at br.com.caelum.vraptor.observer.RequestHandlerObserver.handle(RequestHandlerObserver.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93)
at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266)
at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253)
at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232)
at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169)
at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165)
at org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119)
at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112)
at org.jboss.weld.event.EventImpl.fire(EventImpl.java:83)
at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:123)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Caused by: com.thoughtworks.paranamer.ParameterNamesNotFoundException: One or more @Named annotations missing for class 'java.math.BigDecimal', methodOrCtor <init> and parameter types java.math.BigInteger,int,java.math.MathContext
at com.thoughtworks.paranamer.AnnotationParanamer.lookupParameterNames(AnnotationParanamer.java:129)
at com.thoughtworks.paranamer.CachingParanamer.lookupParameterNames(CachingParanamer.java:90)
at com.thoughtworks.paranamer.CachingParanamer.lookupParameterNames(CachingParanamer.java:83)
at br.com.caelum.vraptor.http.ParanamerNameProvider.parametersFor(ParanamerNameProvider.java:50)
at br.com.caelum.vraptor.http.ParanamerNameProvider$Proxy$_$$_WeldClientProxy.parametersFor(Unknown Source)
at br.com.caelum.vraptor.http.iogi.VRaptorParameterNamesProvider.lookupParameterNames(VRaptorParameterNamesProvider.java:57)
at br.com.caelum.vraptor.http.iogi.VRaptorParameterNamesProvider$Proxy$_$$_WeldClientProxy.lookupParameterNames(Unknown Source)
at br.com.caelum.iogi.reflection.ClassConstructor.<init>(ClassConstructor.java:24)
at br.com.caelum.iogi.reflection.Target.constructors(Target.java:115)
at br.com.caelum.iogi.ObjectInstantiator.instantiate(ObjectInstantiator.java:30)
at br.com.caelum.iogi.MultiInstantiator.instantiate(MultiInstantiator.java:20)
at br.com.caelum.vraptor.http.iogi.VRaptorInstantiator.instantiate(VRaptorInstantiator.java:118)
... 54 more

meu metodo esta assim: 
@Post(value="/constricao/custas")
public void informarCustasComunicado(BigDecimal vlCustas, @NotEmpty String protocolo){

José Filipe Lyra

unread,
Jan 31, 2015, 6:21:48 PM1/31/15
to caelum-...@googlegroups.com
E aí Fabio!

Vc está enviando como um form html normal, com "application/x-www-form-urlencoded" ou você envia um json (application/json)?

Se for json faltou o @Consumes("application/json").

Agora se for formulário normal mesmo seria bom confirmar se o dado está sendo enviado certo para o servidor.

Abs

--
You received this message because you are subscribed to the Google Groups "caelum-vraptor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to caelum-vrapto...@googlegroups.com.
To post to this group, send email to caelum-...@googlegroups.com.
Visit this group at http://groups.google.com/group/caelum-vraptor.
For more options, visit https://groups.google.com/d/optout.

Fabio Ebner

unread,
Jan 31, 2015, 6:35:44 PM1/31/15
to caelum-...@googlegroups.com
Nao estou  fazendo um request ajax lyra, se eu trocar de BigDecimal para Integer ele vai corretamente, ate mesmo se eu mandar o valor 1, ele da erro quando e bigdecimal

José Filipe Lyra

unread,
Jan 31, 2015, 9:12:20 PM1/31/15
to caelum-...@googlegroups.com
Humm, entendi, Fabio.
Vou testar num projeto aqui também. Se eu tiver algum problema compartilho aqui.

Fabio Ebner

unread,
Feb 2, 2015, 4:53:22 PM2/2/15
to caelum-...@googlegroups.com
Lucas e Rodrigo sabem o pq desse erro??

19:46:32,857 DEBUG [ParanamerNameProvider] Found parameter names with paranamer for public void br.com.lumera.integrabrasil.controllers.ComunicadoController.informarCustasComunicado(java.math.BigDecimal,java.lang.String) as [vlCustas, protocolo]
19:46:32,857 DEBUG [IogiParametersProvider] getParametersFor() called with parameters Parameters(Parameter(protocolo -> CP0202000000412715), Parameter(vlCustas -> 123123)) and targets [Target(name=vlCustas, type=class java.math.BigDecimal), Target(name=protocolo, type=class java.lang.String)].
19:46:32,857 DEBUG [DefaultConverters   ] Unable to find a converter for class java.math.BigDecimal. Returning NullConverter.
Fev 02, 2015 7:46:32 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [default] in context with path [/integranovo] threw exception
br.com.caelum.vraptor.http.InvalidParameterException: Exception when trying to instantiate Target(name=vlCustas, type=class java.math.BigDecimal)
at br.com.caelum.vraptor.http.iogi.VRaptorInstantiator.handleException(VRaptorInstantiator.java:128)


estou recebendo um BigDecimal no meu metodo, ja tentei passar 1, 1.2, 1,2 e nao foi em nenhum, se eu mudar o tipo para Integer vai normal

Fabio Ebner

unread,
Feb 2, 2015, 4:56:44 PM2/2/15
to caelum-...@googlegroups.com
So uma coisa, eu criei um converter:


@Specializes
public class MyBigDecimalConverter extends BigDecimalConverter implements JsonDeserializer<BigDecimal>, JsonSerializer<BigDecimal>{

DecimalFormat df = null;
Locale brasil = new Locale("pt", "BR");
@Override
public JsonElement serialize(BigDecimal valor, Type tipo,
JsonSerializationContext context) {
 
        this.df = new DecimalFormat("#,##0.00", new DecimalFormatSymbols(this.brasil));
        this.df.setParseBigDecimal(true);
return new JsonPrimitive(df.format(valor));
}

@Override
public BigDecimal deserialize(JsonElement elemento, Type tipo,
JsonDeserializationContext context) throws JsonParseException {
this.df = new DecimalFormat("#,##0.00", new DecimalFormatSymbols(this.brasil));
        this.df.setParseBigDecimal(true);
try {
return (BigDecimal) df.parse(elemento.getAsString());
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}

}

para que eu receba no formato brasileiro, quando o BigDecimal esta dentro de uma classe ele funciona normalmente. porem quando ela esta direta no metodo nao

To unsubscribe from this group and stop receiving emails from it, send an email to caelum-vraptor+unsubscribe@googlegroups.com.
To post to this group, send email to caelum-vraptor@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "caelum-vraptor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to caelum-vraptor+unsubscribe@googlegroups.com.
To post to this group, send email to caelum-vraptor@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "caelum-vraptor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to caelum-vraptor+unsubscribe@googlegroups.com.
To post to this group, send email to caelum-vraptor@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "caelum-vraptor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to caelum-vraptor+unsubscribe@googlegroups.com.
To post to this group, send email to caelum-vraptor@googlegroups.com.

Lucas Cavalcanti

unread,
Feb 2, 2015, 7:16:46 PM2/2/15
to caelum-vraptor
Isso é bem estranho... no parâmetro não funciona, mas dentro de um objeto funciona.

Tenta receber em uma classe, em outro controller, injetado o Converters e faz um converters.to(BigDecimal.class) e ver se retorna a sua classe.

[]'s
Lucas Cavalcanti
@lucascs

To unsubscribe from this group and stop receiving emails from it, send an email to caelum-vrapto...@googlegroups.com.
To post to this group, send email to caelum-...@googlegroups.com.

Fabio Ebner

unread,
Feb 2, 2015, 7:20:07 PM2/2/15
to caelum-vraptor
como assim nao entendi.

Se eu colocar um bigdecimal dentro de um objeto :

public class muObjeto{
  String nome;
  BigDecimal valor;
}

funciona, porem se eu colocar no metodo do controller nao.

Lucas Cavalcanti

unread,
Feb 2, 2015, 7:25:49 PM2/2/15
to caelum-vraptor
Então, em algum controller crie isso:

@Get("/testando123")
public void teste(Converters converters) {
    System.out.println(converters.to(BigDecimal.class));
}

e veja se imprime a sua classe.

[]'s
Lucas Cavalcanti
@lucascs

Fabio Ebner

unread,
Feb 3, 2015, 5:53:08 AM2/3/15
to caelum-vraptor
Lucas, recebi esse erro: 08:52:25,273 DEBUG [HibernateTransactionInterceptor] tx was rolled back
Fev 03, 2015 8:52:25 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [default] in context with path [/integranovo] threw exception [java.lang.IllegalStateException: Unable to find converter for java.math.BigDecimal] with root cause
java.lang.IllegalStateException: Unable to find converter for java.math.BigDecimal

Rodrigo Turini

unread,
Feb 3, 2015, 7:03:17 AM2/3/15
to caelum-...@googlegroups.com
​tenta tirar o seu ​MyBigDecimalConverter que está especializando e fazer 
novamente? Só pra ver se o BigDecimalConverter nosso vai ser registrado.

Rodrigo Turini
Caelum | Ensino e Inovação
www.caelum.com.br

Rodrigo Turini

unread,
Feb 3, 2015, 7:07:15 AM2/3/15
to caelum-...@googlegroups.com
Ah, agora que vi! Faltou adicionar o @Convert(BigDecimal.class) no seu bean:

@Specializes @Convert(BigDecimal.class)
public class MyBigDecimalConverter extends BigDecimalConverter 
   implements JsonDeserializer<BigDecimal>, JsonSerializer<BigDecimal>{ ... }​

​Quando você especializa, tem que adicionar! Igual no exemplo da nossa doc.​


Rodrigo Turini
Caelum | Ensino e Inovação
www.caelum.com.br

Fabio Ebner

unread,
Feb 3, 2015, 2:50:34 PM2/3/15
to Vraptor
Rodrigo,

agora so de eu chamar o meu login ele ja da esse erro:


  WARN [DefaultEnvironment  ] Could not find the file 'environment.properties' to load.
17:49:22,392 DEBUG [DefaultExceptionMapper] find for exception class java.lang.ClassCastException
17:49:22,395 DEBUG [HibernateTransactionInterceptor] tx was rolled back
Fev 03, 2015 5:49:22 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [default] in context with path [/integranovo] threw exception [java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType] with root cause
java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
    at br.com.caelum.vraptor.serialization.gson.GsonBuilderWrapper.getAdapterType(GsonBuilderWrapper.java:99)
    at br.com.caelum.vraptor.serialization.gson.GsonBuilderWrapper.create(GsonBuilderWrapper.java:74)
    at br.com.caelum.vraptor.serialization.gson.GsonSerializer.serialize(GsonSerializer.java:133)
    at br.com.lumera.integrabrasil.controllers.UsuarioController.loginUsuario(UsuarioController.java:39)
    at br.com.lumera.integrabrasil.controllers.UsuarioController$Proxy$_$$_WeldClientProxy.loginUsuario(Unknown Source)

Fabio Ebner

unread,
Feb 3, 2015, 2:59:55 PM2/3/15
to Vraptor
Rodrigo

Consegui fazendo o seguinte:


@Specializes
@Convert(BigDecimal.class)
public class MyBigDecimalConverter extends BigDecimalConverter {


    DecimalFormat df = null;
    Locale brasil = new Locale("pt", "BR");
   
    @Override
    public BigDecimal convert(String value, Class<? extends BigDecimal> type) {

    this.df = new DecimalFormat("#,##0.00", new DecimalFormatSymbols(this.brasil));
    this.df.setParseBigDecimal(true);
    try {
        return (BigDecimal) df.parse(value);
    } catch (ParseException e) {

        return null;
    }
        //return super.convert(value, type);
    }

}

porem nesse caso eu perco o converter de BigDecimal para String.. pq ja quero enviar os valores com , pontuacao brasileira (1.000,21)

obrigado

Rodrigo Turini

unread,
Feb 3, 2015, 3:20:48 PM2/3/15
to caelum-...@googlegroups.com
Oi Fábio, não sei se entendi bem. Você quer ter que o converter use #,#.# pro 
param vlCustas, e o default atual pra todos os outros casos? Se sim, não pode
passar uma classe Xpto que tem o vlCustas lá dentro e criar o converter p/ ela?

Rodrigo Turini
Caelum | Ensino e Inovação
www.caelum.com.br


Fabio Ebner

unread,
Feb 3, 2015, 3:28:46 PM2/3/15
to Vraptor
Entao eu criei um GsonSerializer


public class MyBigDecimalSerializer implements JsonDeserializer<BigDecimal>, JsonSerializer<BigDecimal>{


    DecimalFormat df = null;
    Locale brasil = new Locale("pt", "BR");
   
    @Override
    public JsonElement serialize(BigDecimal valor, Type tipo,
            JsonSerializationContext context) {
         
        this.df = new DecimalFormat("#,##0.00", new DecimalFormatSymbols(this.brasil));
        this.df.setParseBigDecimal(true);
        return new JsonPrimitive(df.format(valor));
    }

    @Override
    public BigDecimal deserialize(JsonElement elemento, Type tipo,
            JsonDeserializationContext context) throws JsonParseException {
        this.df = new DecimalFormat("#,##0.00", new DecimalFormatSymbols(this.brasil));
        this.df.setParseBigDecimal(true);
        try {
            return (BigDecimal) df.parse(elemento.getAsString());
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

}

Reply all
Reply to author
Forward
0 new messages