使用接口来休息Web服务

时间:2012-03-02 13:30:36

标签: web-services rest jaxb resteasy

我正在尝试使用RestEasy在Java中创建REST Web服务,其设计类似于我在下面显示的示例类:

@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';
    }
}

所以,当我尝试这个设计时,JAXB抱怨如下:

  

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException:   2计数IllegalAnnotationExceptions com.intuit.whitespace.IRelatedInterface是一个接口, JAXB无法处理接口。   此问题与以下位置有关:at com.intuit.whitespace.IRelatedInterface at public java.util.List com.intuit.whitespace.Implementation.getRelations()at com.intuit.whitespace.Implementation com.intuit.whitespace.IRelatedInterface 没有no-arg默认构造函数。此问题与以下位置有关:位于com.intuit.whitespace.Implementation的public java.util.List com.intuit.whitespace.Implementation.getRelations()中的com.intuit.whitespace.IRelatedInterface

我的问题是,有没有办法解决这个问题?我尝试了一些东西,但没有一个有效。我正在考虑使用Spring OXM或基于MessageBodyWriter的解决方案,但我想问一下是否有其他建议可以帮助我更好?

1 个答案:

答案 0 :(得分:0)

好的,我通过进行以下更改解决了这个问题:

使用@XmlElement中的type属性

public class Implementation implements IInterface {

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

就是这样!