English 中文(简体)
如何在RESTEasy 中使用通用模板(<T>)的类中生成 XML 响应?
原标题:How to generate XML Response from Classes with Generic Templates(<T>) in RESTEasy?

我有一个通用服务反应类如下:

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

}

从我的RESTEasy Service, 我想生成 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();

但这是投球错误,因为没有 Jaxb MarshallWriter 用于 java. util. list.

我可以把普通商品类列成清单

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

GenericEntity< serviceResponse< list< Customer> & gt; & gt; 不表示 JaxbMarshallWriter 代表 java. util. list 无效 。

Marshall/unmarshall课程是否有通用模板(, )的工作?

问题回答

我不确定你们班级使用通用模板是否有不同, 但这就是我用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)
    }
}

还有,我还没有测试过这些,希望你有足够的时间 得到图案的图案。

问题不是generic ,问题在于您应该将列表包在对象中。

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

然后,您可以对响应数据类进行注释,以代表一组对象。

我为同一问题做了一个解决办法,就是创建一种新的类型来模拟通用类清单,就像我所做的那样,我创建了一种新类型,我命名为集装箱(例如:人文集装箱),其中列有我实体(个人)的清单,我使用的是清单,而不是清单类型,而且非常有效。

我举个例子, 如果能对你有用的话:

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 +  } ;
    }
}

这是集装箱管理员:

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

我知道答复时间太晚,但由于没有经投票表决的回答,我将尽量希望回答会有所帮助。

问题是,当你有一个通用类时,MyClasss jaxB Expluss T会用 @XMLROUTElement 或@XMLType 附加说明。

in your code scenario your type T is of List List does not have any of @XMLRootElement or @XMLType so it throws error. I think the solution for above case is create a wrapper class for Collection like

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签