Java 如何使用Jersey将嵌套列表封送为JSON?我得到一个空数组或一个包含数组的单元素字典数组
我正在从事一个项目,该项目使用Jersey将对象转换为JSON。我希望能够写出嵌套列表,如下所示:Java 如何使用Jersey将嵌套列表封送为JSON?我得到一个空数组或一个包含数组的单元素字典数组,java,arrays,json,jaxb,jersey,Java,Arrays,Json,Jaxb,Jersey,我正在从事一个项目,该项目使用Jersey将对象转换为JSON。我希望能够写出嵌套列表,如下所示: {"data":[["one", "two", "three"], ["a", "b", "c"]]} 我要转换的对象首先表示为数据,我认为Jersey会做正确的事情。以上内容作为空值列表输出: {"data":[null, null]} 在了解到嵌套对象需要包装后,我尝试了以下操作: @XmlRootElement(name = "foo") @XmlType(propOrder = {"d
{"data":[["one", "two", "three"], ["a", "b", "c"]]}
我要转换的对象首先表示为数据,我认为Jersey会做正确的事情。以上内容作为空值列表输出:
{"data":[null, null]}
在了解到嵌套对象需要包装后,我尝试了以下操作:
@XmlRootElement(name = "foo")
@XmlType(propOrder = {"data"})
public class Foo
{
private Collection<FooData> data = new LinkedList<FooData>();
@XmlElement(name = "data")
public Collection<FooData> getData()
{
return data;
}
public void addData(Collection data)
{
FooData d = new FooData();
for(Object o: data)
{
d.getData().add(o == null ? (String)o : o.toString());
}
this.data.add(d);
}
@XmlRootElement(name = "FooData")
public static class FooData
{
private Collection<String> data = new LinkedList<String>();
@XmlElement
public Collection<String> getData()
{
return data;
}
}
}
我希望第一个数据是一个列表列表,而不是一个元素字典列表。我如何做到这一点
以下是我的JAXBContentResolver:
@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext>
{
private JAXBContext context;
private Set<Class<?>> types;
// Only parent classes are required here. Nested classes are implicit.
protected Class<?>[] classTypes = new Class[] {Foo.class};
protected Set<String> jsonArray = new HashSet<String>(1) {
{
add("data");
}
};
public JAXBContextResolver() throws Exception
{
Map<String, Object> props = new HashMap<String, Object>();
props.put(JSONJAXBContext.JSON_NOTATION, JSONJAXBContext.JSONNotation.MAPPED);
props.put(JSONJAXBContext.JSON_ROOT_UNWRAPPING, Boolean.TRUE);
props.put(JSONJAXBContext.JSON_ARRAYS, jsonArray);
this.types = new HashSet<Class<?>>(Arrays.asList(classTypes));
this.context = new JSONJAXBContext(classTyes, props);
}
public JAXBContext getContext(Class<?> objectType)
{
return (types.contains(objectType)) ? context : null;
}
}
@Provider
公共类JAXBContextResolver实现ContextResolver
{
私有JAXBContext上下文;
私有集[]类类型=新类[]{Foo.Class};
受保护集jsonArray=新哈希集(1){
{
添加(“数据”);
}
};
公共JAXBContextResolver()引发异常
{
Map props=newhashmap();
put(JSONJAXBContext.JSON_NOTATION,JSONJAXBContext.JSONNotation.MAPPED);
put(JSONJAXBContext.JSON_ROOT_UNWRAPPING,Boolean.TRUE);
put(JSONJAXBContext.JSON_数组,jsonArray);
this.types=newhashset您尝试过jersey json吗
将jersey json添加到类路径(或maven依赖项)
然后用这个:
@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private final JAXBContext context;
public JAXBContextResolver() throws Exception {
this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), "package.of.your.model");
}
public JAXBContext getContext(Class<?> objectType) {
return context;
}
}
查看本页的“改进应用程序”部分:
我知道这个问题很老,但我偶然发现了一个类似的问题,但我想呈现一个数组列表,即“列表”,因为我从jpa获得的一个db和一个不使用实体的nativ查询的结果
我就是这样解决的:
首先创建了ListWrapper.java:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class ListWrapper extends ArrayList {
@SuppressWarnings("unchecked")
public ListWrapper() {
super();
}
public ListWrapper(List list) {
super(list);
}
}
然后我创建了一个扩展AbstractMessageReaderWriterProvider的类
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider;
@Provider
@Produces("*/*")
@Consumes("*/*")
public class ListObjectArrayMessagereaderWriterProvider extends AbstractMessageReaderWriterProvider<ListWrapper> {
public boolean supports(Class type) {
return type == ListWrapper.class;
}
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == ListWrapper.class;
}
@Override
public ListWrapper readFrom(Class<ListWrapper> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
throw new IllegalArgumentException("Not implemented yet.");
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == ListWrapper.class;
}
@SuppressWarnings("unchecked")
@Override
public void writeTo(ListWrapper t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
final Iterator<Object[]> iterator = t.iterator();
OutputStreamWriter writer = new OutputStreamWriter(entityStream, getCharset(mediaType));
final JSONArray jsonArrayOuter = new JSONArray();
while (iterator.hasNext()) {
final Object[] objs = iterator.next();
JSONArray jsonArrayInner = new JSONArray(Arrays.asList(objs));
jsonArrayOuter.put(jsonArrayInner);
}
try {
jsonArrayOuter.write(writer);
writer.write("\n");
writer.flush();
} catch (JSONException je) {
throw new WebApplicationException(new Exception(ImplMessages.ERROR_WRITING_JSON_ARRAY(), je), 500);
}
}
}
搜索方法返回一个包含对象[]列表的Listwrapper
希望这对某人有所帮助:-)
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class ListWrapper extends ArrayList {
@SuppressWarnings("unchecked")
public ListWrapper() {
super();
}
public ListWrapper(List list) {
super(list);
}
}
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider;
@Provider
@Produces("*/*")
@Consumes("*/*")
public class ListObjectArrayMessagereaderWriterProvider extends AbstractMessageReaderWriterProvider<ListWrapper> {
public boolean supports(Class type) {
return type == ListWrapper.class;
}
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == ListWrapper.class;
}
@Override
public ListWrapper readFrom(Class<ListWrapper> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
throw new IllegalArgumentException("Not implemented yet.");
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == ListWrapper.class;
}
@SuppressWarnings("unchecked")
@Override
public void writeTo(ListWrapper t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
final Iterator<Object[]> iterator = t.iterator();
OutputStreamWriter writer = new OutputStreamWriter(entityStream, getCharset(mediaType));
final JSONArray jsonArrayOuter = new JSONArray();
while (iterator.hasNext()) {
final Object[] objs = iterator.next();
JSONArray jsonArrayInner = new JSONArray(Arrays.asList(objs));
jsonArrayOuter.put(jsonArrayInner);
}
try {
jsonArrayOuter.write(writer);
writer.write("\n");
writer.flush();
} catch (JSONException je) {
throw new WebApplicationException(new Exception(ImplMessages.ERROR_WRITING_JSON_ARRAY(), je), 500);
}
}
}
@GET
@Path("/{id}/search")
@Produces(JSON)
public ListWrapper search(@PathParam("id") Integer projectId ) {
return DatabaseManager.search(projectId);
}