在 JSON 和 XML 中使用泛型的 Jersey Jackson 自定义响应
Jersey Jackson Custom Response with Generics in JSON and XML
一段时间以来,我一直在尝试想出一种方法来 return 自定义响应 object,其数据属性可以是 object 或列表。这里有一些简化的相关 classes。我正在使用 Jersey v2.27 和 Jackson v2.9+
@XmlRootElement(name = "response")
public class ResponseEnvelope<T> {
private Metadata metadata;
private T data;
public ResponseEnvelope(Metadata metadata, T data) {
this.metadata = metadata;
this.data = data;
}
}
@XmlRootElement
public class Person {
private String name;
private int age;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
}
@XmlRootElement
public class ListWrapper<T> {
private List<T> list;
public ListWrapper(List<T> list) {this.list = list;}
}
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public class PersonResource {
private List<Person> getPeople() {
return Arrays.asList(new Person(20, "Monty Python"), new Person(25, "Brian");
}
@GET
public Response getAllPeople() {
ResponseEnvelope<ListWrapper<Person> response = new ResponseEnvelope<ListWrapper<Person>>(new Metadata(), new ListWrapper(getPeople());
return Response.status(200).entity(response).build();
}
@GET
@Path("{id}")
public Response getExampleById(@PathParam("id") String id) {
ResponseEnvelope<Person> response = new ResponseEnvelope<Person>(new Metadata(), getPeople().get(0));
return Response.status(200).entity(response).build();
}
}
@Provider
@Produces(MediaType.APPLICATION_XML)
public class CustomResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;
public CustomResolver() {
try {
this.context = JAXBContext.newInstance(
ArrayList.class,
Person.class,
ListWrapper.class,
ResponseEnvelope.class
);
}
catch (JAXBException e) {
throw new RuntimeException(e);
}
}
@Override
public JAXBContext getContext(Class<?> type) {
return (type.equals(ResponseEnvelope.class)) ? context : null;
}
}
@ApplicationPath("")
public class ServerConfig extends ResourceConfig {
public ServerConfig() {
this.packages(true,"com.example");
this.register(JacksonFeature.class);
}
}
我已经尝试了几个 tutorials/questions 的东西,例如 here, here, here 等(我已经尝试了很多...)
我似乎无法在两种格式之间得到统一的回应。当我尝试将 List
直接用作 T data
时,JAXB 抱怨没有 ArrayList 的上下文(当我把它放在那里时,它不会输出元素本身)。
当我在响应中直接使用GenericEntity
class时,比如with
GenericEntity<List<Person>> list = new GenericEntity<List<Person>>(people){};
return Response.status(200).entity(list).build();`
json 和 xml 的输出都是正确的。但是,如果我在我的 T data
字段中使用它,我会得到一个 JAXB 异常,它找不到我的资源 class (似乎你不能在通用 class?).
如果我使用通用 ListWrapper
class 来放入我的 collections,我就能够创建有效的 xml 和 json,并且使用各种注释(@XmlAnyElement(lax = true)
或 @XmlElements({@XmlElement(name = "person", type = Person.class)}
)我可以正确格式化 XML,但 json 包含额外的 ListWrapper<T> list
属性(即{"data": {"list": []}}
,而不是 {"data": []}
。我试过为此使用 @JsonUnwrapped
,并将 @JsonSerialize
与自定义序列化程序一起使用,但我似乎无法将其展平。
针对唯一人员 ID 的响应的所需示例响应
{
"metadata": {
// some content
},
"data": {
"age": 20,
"name": "Monty Python"
}
}
和XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
<data>
<age>20</age>
<name>Monty Python</name>
</data>
<metadata>
<!-- some content -->
</metadata>
</response>
以及人员列表:
{
"metadata": {
// some content
},
"data": [
{
"age": 20,
"name": "Monty Python"
},
{
"age": 25,
"name": "Brian"
}
]
}
和XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
<data>
<person>
<age>20</age>
<name>Monty Python</name>
</person>
<person>
<age>25</age>
<name>Brian</name>
</person>
</data>
<metadata>
<!-- some content -->
</metadata>
</response>
答案是使用 ListWrapper<T>
class,并用 @JsonValue
和 @XmlAnyElement(lax = true)
注释 getter。这些创建格式正确的 JSON 和 XML 响应。 class 应该是这样的
import java.util.List;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonValue;
@XmlRootElement(name = "data")
public class ListWrapper<T> {
private List<T> list;
public ListWrapper() {}
public ListWrapper(List<T> list) {
this.list = list;
}
/**
* @return the list
*/
@XmlAnyElement(lax = true)
@JsonValue
public List<T> getList() {
return list;
}
/**
* @param list
* the list to set
*/
public void setList(List<T> list) {
this.list = list;
}
}
一段时间以来,我一直在尝试想出一种方法来 return 自定义响应 object,其数据属性可以是 object 或列表。这里有一些简化的相关 classes。我正在使用 Jersey v2.27 和 Jackson v2.9+
@XmlRootElement(name = "response")
public class ResponseEnvelope<T> {
private Metadata metadata;
private T data;
public ResponseEnvelope(Metadata metadata, T data) {
this.metadata = metadata;
this.data = data;
}
}
@XmlRootElement
public class Person {
private String name;
private int age;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
}
@XmlRootElement
public class ListWrapper<T> {
private List<T> list;
public ListWrapper(List<T> list) {this.list = list;}
}
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public class PersonResource {
private List<Person> getPeople() {
return Arrays.asList(new Person(20, "Monty Python"), new Person(25, "Brian");
}
@GET
public Response getAllPeople() {
ResponseEnvelope<ListWrapper<Person> response = new ResponseEnvelope<ListWrapper<Person>>(new Metadata(), new ListWrapper(getPeople());
return Response.status(200).entity(response).build();
}
@GET
@Path("{id}")
public Response getExampleById(@PathParam("id") String id) {
ResponseEnvelope<Person> response = new ResponseEnvelope<Person>(new Metadata(), getPeople().get(0));
return Response.status(200).entity(response).build();
}
}
@Provider
@Produces(MediaType.APPLICATION_XML)
public class CustomResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;
public CustomResolver() {
try {
this.context = JAXBContext.newInstance(
ArrayList.class,
Person.class,
ListWrapper.class,
ResponseEnvelope.class
);
}
catch (JAXBException e) {
throw new RuntimeException(e);
}
}
@Override
public JAXBContext getContext(Class<?> type) {
return (type.equals(ResponseEnvelope.class)) ? context : null;
}
}
@ApplicationPath("")
public class ServerConfig extends ResourceConfig {
public ServerConfig() {
this.packages(true,"com.example");
this.register(JacksonFeature.class);
}
}
我已经尝试了几个 tutorials/questions 的东西,例如 here, here, here 等(我已经尝试了很多...)
我似乎无法在两种格式之间得到统一的回应。当我尝试将 List
直接用作 T data
时,JAXB 抱怨没有 ArrayList 的上下文(当我把它放在那里时,它不会输出元素本身)。
当我在响应中直接使用GenericEntity
class时,比如with
GenericEntity<List<Person>> list = new GenericEntity<List<Person>>(people){};
return Response.status(200).entity(list).build();`
json 和 xml 的输出都是正确的。但是,如果我在我的 T data
字段中使用它,我会得到一个 JAXB 异常,它找不到我的资源 class (似乎你不能在通用 class?).
如果我使用通用 ListWrapper
class 来放入我的 collections,我就能够创建有效的 xml 和 json,并且使用各种注释(@XmlAnyElement(lax = true)
或 @XmlElements({@XmlElement(name = "person", type = Person.class)}
)我可以正确格式化 XML,但 json 包含额外的 ListWrapper<T> list
属性(即{"data": {"list": []}}
,而不是 {"data": []}
。我试过为此使用 @JsonUnwrapped
,并将 @JsonSerialize
与自定义序列化程序一起使用,但我似乎无法将其展平。
针对唯一人员 ID 的响应的所需示例响应
{
"metadata": {
// some content
},
"data": {
"age": 20,
"name": "Monty Python"
}
}
和XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
<data>
<age>20</age>
<name>Monty Python</name>
</data>
<metadata>
<!-- some content -->
</metadata>
</response>
以及人员列表:
{
"metadata": {
// some content
},
"data": [
{
"age": 20,
"name": "Monty Python"
},
{
"age": 25,
"name": "Brian"
}
]
}
和XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
<data>
<person>
<age>20</age>
<name>Monty Python</name>
</person>
<person>
<age>25</age>
<name>Brian</name>
</person>
</data>
<metadata>
<!-- some content -->
</metadata>
</response>
答案是使用 ListWrapper<T>
class,并用 @JsonValue
和 @XmlAnyElement(lax = true)
注释 getter。这些创建格式正确的 JSON 和 XML 响应。 class 应该是这样的
import java.util.List;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonValue;
@XmlRootElement(name = "data")
public class ListWrapper<T> {
private List<T> list;
public ListWrapper() {}
public ListWrapper(List<T> list) {
this.list = list;
}
/**
* @return the list
*/
@XmlAnyElement(lax = true)
@JsonValue
public List<T> getList() {
return list;
}
/**
* @param list
* the list to set
*/
public void setList(List<T> list) {
this.list = list;
}
}