2012-03-02 122 views
1

,我試圖建立在Java中使用RestEasy的類似的例子類設計一個REST Web服務,我如下圖所示: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無法處理接口。 此問題與以下位置有關:com.intuit.whitespace.IRelatedInterface public java.util.List com.intuit.whitespace.Implementation.getRelations()at com.intuit.whitespace.Implementation com.intuit.whitespace。 IRelatedInterface 沒有無參數默認構造函數。這個問題涉及到以下位置:在com.intuit.whitespace.Implementation

在com.intuit.whitespace.IRelatedInterface在公衆的java.util.List com.intuit.whitespace.Implementation.getRelations()我問題是,有沒有辦法解決這個問題?我嘗試了一些東西,但沒有一個能工作。我正在考慮Spring OXM或基於MessageBodyWriter的解決方案,但我想問問是否有其他建議可以幫助我更好?

+0

我要去嘗試http://stackoverflow.com/questions/4931960/jersey-rest-jaxb-error-mapping-an-interface,因爲當我搜索類似的問題時沒有出現。 – sinha 2012-03-02 13:32:59

回答

0

好吧,我解決它通過進行以下更改:

用於@XmlElement

public class Implementation implements IInterface { 

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

type屬性這就是全部!