I created a handler for parameters that were json-encoded, but I
"generefied" it to make it applicable for various classes just by
subclassing it. Unfortunately it does not work as expected, unless
applying a little hack. I would like to know why the hack (to be
described) is necessary (if at all).
This is basically the general json handler:
public class GeneralJsonHandler<T> implements ParameterHandler<T> {
/** Deserializes an Object of class T from its JSON representation */
final public T fromString(String jsonRepresentation) {
ObjectMapper mapper = new ObjectMapper();
T o= null;
try {
o = mapper.readValue(jsonRepresentation,
getGenericClass() );
} catch (IOException e) {
throw new WebApplicationException()
}
return o;
}
}
By my logic, simply subclassing this with the right type parameter T
should essentially create a ParameterHandler for a type T.
public class FooHandler extends GeneralJsonHandler<Foo>{ }
public class BarHandler extends GeneralJsonHandler<Bar>{ }
After registering these two handlers I should be able to handle
json-ified string representations of Foo and Bar. Unfortunately,
either is likely to be picked by CXF to handle a json string, no
matter what class the string is supposed to represent. To make CXF
aware that they are only applicable to the parameterized type I was
forced to specify that they implement ParameterHandler<T>, something I
found totally unnecessary, as that info is already implicitly given by
the superclass.
public class FooHandler extends GeneralJsonHandler<Foo> implements
ParameterHandler<Foo> { }
public class BarHandler extends GeneralJsonHandler<Bar> implements
ParameterHandler<Bar> { }{ }
Any clues as to why the first approach did not work?
--
Carl-Erik Kopseng