java - How to create a ParamConverter<Integer> that handles primitive int as well? -


so want create custom paramconverter<integer> in jax-rs:

public class intparamconverter implements paramconverter<integer>{      @override     public integer fromstring(string value) {         try{             return integer.valueof(value);         }catch (exception e){             //do stuff             throw new badrequestex("convert exception");         }     }     @override     public string tostring(integer value) {         return string.valueof(value);     } } 

and register via paramconverterprovider:

@provider public class customparamconverterhandler implements paramconverterprovider {          @override         public <t> paramconverter<t> getconverter(final class<t> rawtype, type generictype, annotation[] annotations) {             if (rawtype.equals(integer.class)){                 return (paramconverter<t>) new intparamconverter();             }             return null;         }     } 

and use in resource pojo follows:

    @get     @path("/conv")     @produces({mediatype.text_plain})     public string conv(@queryparam("no") int no) {         return ""+no;     } 

in runtime (testing system.out) find out tomee (apache cfx?) not use intparamconverter. if instead declare @queryparam("no") integer no opposed @queryparam("no") int nothen works fine (tomee uses custom paramconverter.

i tried registering intparamconverter primitive int:

if (rawtype.equals(int.class)){      return (paramconverter<t>) new intparamconverter(); } 

but runtime exception occures:

java.lang.classcastexception: cannot cast java.lang.integer int     @ java.lang.class.cast(class.java:3369)     @ org.apache.cxf.jaxrs.utils.injectionutils.handleparameter(injectionutils.java:388)     @ org.apache.cxf.jaxrs.utils.injectionutils.createparameterobject(injectionutils.java:978)     @ org.apache.cxf.jaxrs.utils.jaxrsutils.readquerystring(jaxrsutils.java:1210) 

how can create such paramconverter handles primitive int?

you can't.

java generics converted java.lang.object on compilation (by process called type erasure).

the primitive types such int not have java.lang.object base class such conversion not possible in such cases.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -