1

I am trying to create a REST web-service in Java using RestEasy with the design similar to the example classes that I have shown below:

@Path("/rest")
public class TestRestService {
    @GET
    @Path("/test")
    @Produces("application/xml")
    public Response sayTestXml() {
        return getImplementation();
    }

    public Response getImplementation() {
        IInterface refToImpl = TestService.getImplementation(); 
        return Response.status(Status.OK).entity(refToImpl).build();
    }
}

public class TestService {
    public static IInterface getImplementation() {
        IInterface ref = new Implementation();
        return ref;
    }
}

public interface IInterface {
    public long getLong();
    public String getString();
    public boolean getBoolean();
    public List<IRelatedInterface> getRelations();
}

public interface IRelatedInterface {
    public float getFloat();
    public char getChar();
    public byte getByte();  
}

@XmlRootElement(name="interface")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Implementation implements IInterface {

    @XmlElement(name="tlong",  required=true)
        public long getLong() {
        return 42;
    }

    @XmlElement(name="tstring", required=true)
    public String getString() {
        return "test";
    }

    @XmlElement(name="tboolean", required=true)
    public boolean getBoolean() {
        return false;
    }

    @XmlElementWrapper(name = "relations")
    @XmlElement(name = "relation", required=false)
    public List<IRelatedInterface> getRelations() {

        List<IRelatedInterface> list = new ArrayList<IRelatedInterface>();
        RelatedImplementation impl = new RelatedImplementation();
        list.add(impl);

        return list;
    }
}

@XmlRootElement(name="relatedInterface")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class RelatedImplementation implements IRelatedInterface {

    @XmlElement(name="tfloat",  required=true)
    public float getFloat() {
        return 1.23f;
    }

    @XmlElement(name="tchar",  required=true)
    public char getChar() {
        return 'A';
    }

    @XmlElement(name="tbyte",  required=true)
    public byte getByte() {
        return 'Z';
    }
}

So, when I try this design then JAXB complains as below:

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions com.intuit.whitespace.IRelatedInterface is an interface, and JAXB can't handle interfaces. this problem is related to the following location: at com.intuit.whitespace.IRelatedInterface at public java.util.List com.intuit.whitespace.Implementation.getRelations() at com.intuit.whitespace.Implementation com.intuit.whitespace.IRelatedInterface does not have a no-arg default constructor. this problem is related to the following location: at com.intuit.whitespace.IRelatedInterface at public java.util.List com.intuit.whitespace.Implementation.getRelations() at com.intuit.whitespace.Implementation

My question is, is there a way to solve this? I have tried some things but none of them have worked. I am considering Spring OXM or a MessageBodyWriter based solution, but I wanted to ask if there were any other suggestions that would help me better?

1

1 Answer 1

0

Okay, I solved it by making the following changes:

Used the type attribute in @XmlElement

public class Implementation implements IInterface {

    @XmlElementWrapper(name = "relations")
    @XmlElement(name = "relation", required=false, type=RelatedImplementation.class)
    public List<IRelatedInterface> getRelations() {
        ....
    }
}

That is all!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.