gpt4 book ai didi

java - 使用 JSON 和 XML 中的泛型的 Jersey Jackson 自定义响应

转载 作者:行者123 更新时间:2023-12-03 19:24:58 25 4
gpt4 key购买 nike

一段时间以来,我一直在尝试想出一种方法来返回自定义响应对象,该对象的数据属性可以是对象或列表。这里有一些简化的相关类。我正在使用 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);
}
}

我已经尝试了几个教程/问题,例如 here , here , here等等(我已经尝试了很多...)

我似乎无法在两种格式之间得到统一的回应。当我尝试使用 List直接作为T data ,JAXB 提示没有 ArrayList 的上下文(当我把它放在那里时,它不会输出元素本身)。

当我使用 GenericEntity直接在响应中类,例如 with

 GenericEntity<List<Person>> list = new GenericEntity<List<Person>>(people){};
return Response.status(200).entity(list).build();`

json 和 xml 的输出都是正确的。但是,如果我在我的 T data 中使用它字段,我得到一个 JAXB 异常,它找不到我的资源类(看来你不能在泛型类中使用 GenericEntity?)。

如果我使用通用 ListWrapper类来放入我的集合,我能够创建有效的 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>类,并用 @JsonValue 注释 getter , 以及 @XmlAnyElement(lax = true) .这些创建格式正确的 JSON 和 XML 响应。该类应该如下所示

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

关于java - 使用 JSON 和 XML 中的泛型的 Jersey Jackson 自定义响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50707574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com