NPE when using a generic extension in JAXB / Resteasy

I am using RestEasy 2.3.4. I am facing NPE. I have a common base class ValueContainer, which I plan to extend as StringValue, IntegerValue, BooleanValue, etc., Replacing the general parameter with the corresponding type.

I am experimenting using extension classes as data transfer objects, as shown in the following. However, I encounter NPE when handling JAXB. This means that I am not considering extension classes in JAXB correctly. Please advise. What is the right way to do this? Thanks.

Base class:

@XmlAccessorType(XmlAccessType.FIELD) public class ValueContainer<T> { @XmlAttribute protected T value; public ValueContainer() { } public ValueContainer(T value) { this.value = value; } public T getValue() { return value; } public void setValue(T value) { this.value = value; } } 

An example of an extension class:

 @XmlRootElement (name="integervalue") public class IntegerValue extends ValueContainer<Integer> { public IntegerValue() { } public IntegerValue(Integer value) { super(value); } } 

Jax-rs API:

 @GET @Path("/status/{what}") IntegerValue getStatus(@PathParam ("what")Integer what) ; 

Impl API:

 @Override public IntegerValue getStatus(Integer what) { return new IntegerValue(what); } 

The reason is NPE where config returns null when my browser calls http: ... / status / 1:

 package org.jboss.resteasy.plugins.providers.jaxb; ..... protected JAXBContext createContextObject(Annotation[] parameterAnnotations, Class... classes) throws JAXBException { JAXBConfig config = FindAnnotation.findAnnotation(parameterAnnotations, JAXBConfig.class); return new JAXBContextWrapper(config, classes); } 
+4
source share
1 answer

I think you should throw an exception in public ValueContainer() . This ctor exists only because JAXB requires that we have public no-arg constructors (see Why does JAXB need a public no-arg constructor for? ). It happens that your object is created without any arguments, and the value remains zero (strange, your Java compiler does not complain).

0
source

Source: https://habr.com/ru/post/1447856/


All Articles