2012-05-25 32 views
9

我有一个通用的ServiceResponse类,如下所示:如何使用RESTEasy中的通用模板类(<T>)生成XML响应?

@XMLRootElement 
public class ServiceResponse<T> 
{ 
    private T data; 
    private String error; 
    //setters n getters 

} 

从我的RESTEasy服务,我想生成XML响应为:

List<Customer> customers = someDAO.getCustomers(); 
ServiceResponse<List<Customer>> resp = new ServiceResponse<List<Customer>>(); 
resp.setData(customers); 
resp.setError("No Error"); 
return resp; 
or return Response.ok().entity(resp).build(); 

但这是抛出错误,因为是对Java没有JaxbMarshallWriter .util.List。

我可以列表列举usinig GenericEntity类。

GenericEntity<List<Customer>> entity = new GenericEntity<List<Customer>>(customers){}; 
Response.ok(entity).build(); 

GenericEntity<ServiceResponse<List<Customer>>>工作不说不JaxbMarshallWriter为java.util.List的。

是否有任何解决通用模板(,)的marshall/unmarshall类?

+0

也许这有帮助吗? http://stackoverflow.com/questions/5391486/make-a-collection-generic-in-javax-xml-bind – Friso

回答

0

问题不是通用问题是你应该将你的列表包裹在一个对象中。

ServiceResponse<ResponseData<Customer>> resp = new ServiceResponse<ResponseData<Customer>>(); 

然后,您可以注释ResponseData类来表示一组对象。

0

我为同样的问题所做的一个解决方案是创建一个新类型来模拟泛型类型List,就像我所做的那样,我创建了一个名为Container的新类型(例如:PersonContainer)其中有我的实体(人),我使用了列表式的,而不是一个列表,它工作得很好......

这里有我的例子,如果它能够对您有用:

package com.dosideals.server.beans; 

import java.io.Serializable; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.xml.bind.annotation.XmlRootElement; 

/** 
* 
* @author LOTFI 
*/ 
@Entity 
@XmlRootElement 
public class Admin implements Serializable { 

    @Id 
    private String login; 
    private String password; 
    private String firstName; 
    private String lastName; 

    public Admin() { 
    } 

    public Admin(String login, String password, String firstName, String lastName) { 
     this.login = login; 
     this.password = password; 
     this.firstName = firstName; 
     this.lastName = lastName; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public String getLogin() { 
     return login; 
    } 

    public void setLogin(String login) { 
     this.login = login; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (obj == null) { 
      return false; 
     } 
     if (getClass() != obj.getClass()) { 
      return false; 
     } 
     final Admin other = (Admin) obj; 
     if ((this.login == null) ? (other.login != null) : !this.login.equals(other.login)) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 7; 
     hash = 83 * hash + (this.login != null ? this.login.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public String toString() { 
     return "Admin{" + "login=" + login + ", password=" + password + ", firstName=" + firstName + ", lastName=" + lastName + '}'; 
    } 
} 

而这是容器AdminContainer:

package com.dosideals.server.beans.containers; 

import com.dosideals.server.beans.Admin; 
import java.util.List; 
import javax.xml.bind.annotation.XmlRootElement; 

/** 
* 
* @author LOTFI 
*/ 
@XmlRootElement 
public class AdminContainer { 

    private List<Admin> admin; 

    public AdminContainer() { 
    } 

    public AdminContainer(List<Admin> admin) { 
     this.admin = admin; 
    } 

    public List<Admin> getAdmin() { 
     return admin; 
    } 

    public void setAdmin(List<Admin> admin) { 
     this.admin = admin; 
    } 
} 
1

我不知道,如果它使你的类使用通用模板有差别,但是这是我将如何使用的RESTEasy

这是会牵着你的服务响应类生成一个XML响应

public class ServiceResponse<T> 
{ 
    private T data; 
    private String error; 
    //setters n getters 
} 

这是实际将您的响应转换为XML的类。除了接收和生成XML/JSON或您正在使用的任何其他类,此类实际上并没有太多的工作。然后它将请求传递给做真正工作的类。然而,这是能够回答你的具体问题的班级(我相信)。

@Path("/myrestservice") 
public class SomeRestService 
{ 
    private SomeCoreService coreService; 
    //getters and setters here 

    @POST 
    @Path("/examples/") 
    @Consumes({MediaType.APPLICATION_XML}) //this consumes XML 
    @Produces({MediaType.APPLICATION_XML}) //this produces XML 
    public ServiceResponse<T> exampleFunction(Request request) 
    { 
     try 
     { 
      //Unwrap the request and take only what you need out 
      //of the request object here 
      return this.coreService.examples(request.getStringFromRequest()); 
     } 
     catch(Exception ex) 
     { 
      return new ServiceResponse<T>(Put response error message here); 
     } 
    } 
} 

这是能够完成所有实际工作的班级。

public class SomeCoreService 
{ 
    public ServiceResponse<T> examples(String stringFromRequest) 
    { 
     //do whatever work you need to do here. 
     return new ServiceResponse<T>(put whatever you need in the service response here) 
    } 
} 

另外,我还没有测试过这些。希望你能够得到这个模式就足够了。

0

我知道很迟才回复,但由于没有投票答案,我会尽量给我的答案,希望它有帮助。

问题是当你有一个泛型类说,MyClass jaxB除了T用@XMLRootElement或@XMLType注释。

在您的代码场景中,您的类型T是List List,没有任何@XMLRootElement或@XMLType,因此会引发错误。 我觉得对于上述情况的解决方案是创建一个集合的包装类像

@XMLRootElement 
Class JaxBCollection<T>{ 
    java.util.Collection<T> collection; 
    /* Have getters and setters*/ 
} 
现在

在你的代码有这样的事情。

List<Customer> customers = someDAO.getCustomers(); 
JaxBCollection<Customer> jaxBCustomers= new JaxBCollection<Customer>(); 
jaxBCustomers.setCollection(customers); 
ServiceResponse<JaxBCollection<Customer>> resp = new ServiceResponse<JaxBCollection<Customer>>(); 
resp.setData(jaxBCustomers); 
resp.setError("No Error"); 
return resp; 
相关问题