【发布时间】:2014-10-10 15:04:43
【问题描述】:
我正在寻找一种方法来包装我的服务的任何响应。
我有一个简单的服务,有很多不同的方法,就像这样
@Path(value = "/listSomething/{apiKey:.+}")
public List<ObjectA> listSomething(@PathParam(value = "apiKey") String apiKey)
一些返回列表,一些只是一些简单的对象。我现在想要实现的是用一些状态信息(例如下面)包装任何围绕它们的响应。
response {
staus: "OK",
data: {..the actual response..}
}
我尝试使用一些拦截器来实现这一点,但没有成功(除非我添加 @XmlSeeAlso 注释,否则我真的不想要什么,因为我想要一些通用的方法)。我的包装类看起来像:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
//@XmlSeeAlso(ResponseA.class)
public class ResponseWrapper {
boolean error;
String message;
@XmlAnyElement(lax = true)
Object object;
public boolean isError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
public String getMessage() {
return message;
}
}
我尝试了@XmlElement、@XmlAnyElement 的不同组合 - 没有任何成功。我在 WriterInterceptor 和 ContainerResponseFilter 中尝试过。
我该如何做这个简单的技巧(如果我不必关心消费者是否需要 XML 或 JSON,那会很酷,但如果我必须为 JSON 破解一些东西,我可以接受)?
我现在感觉有点失落。谢谢你的帮助。
我尝试过的方法通常遇到的错误是 UNKOWN_CLASS
{0} nor any of its super class is known to this context.
我的拦截器或多或少会这样做
responseContext.setGenericType(Wrapper.class);
responseContext.setType(Wrapper.class);
responseContext.setEntity(wrapperInstance);
【问题讨论】:
标签: jaxb jax-ws jax-rs jersey-2.0