我正在实现一个示例 here
我需要输出 json 来命名数组。
{"files":[]}
而不是我当前得到的输出 {[]}
。我需要做什么才能将名称添加到数组中?
@GET
@Path("/{key}/meta")
public Response redirect(@PathParam("key") String key) throws IOException {
BlobKey blobKey = new BlobKey(key);
BlobInfo info = blobInfoFactory.loadBlobInfo(blobKey);
String name = info.getFilename();
long size = info.getSize();
String url = "/rest/file/" + key;
FileMeta meta = new FileMeta(name, size, url);
List<FileMeta> metas = Lists.newArrayList(meta);
GenericEntity<List<FileMeta>> entity = new GenericEntity<List<FileMeta>>(metas) {};
return Response.ok(entity).build();
}
最佳答案
您需要您的实体类包含 List<FileMeta>
的实例叫files
获得 JSON 输出。
public Class EntityClass
{
private List<FileMeta> files;
//Getter and Setter Methods.
}
这是 redirect
中您需要的内容方法。
@GET
@Path("/{key}/meta")
@Produces(MediaType.APPLICATION_JSON)
public Response redirect(@PathParam("key") String key) throws IOException {
BlobKey blobKey = new BlobKey(key);
BlobInfo info = blobInfoFactory.loadBlobInfo(blobKey);
String name = info.getFilename();
long size = info.getSize();
String url = "/rest/file/" + key;
FileMeta meta = new FileMeta(name, size, url);
List<FileMeta> meta = Lists.newArrayList(meta);
EntityClass entity= new EntityClass();
entity.setFiles(meta);
return Response.ok(entity).build();
}
PS:此外,您需要在 web.xml 中配置 POJOMapping。
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
关于java - 使用 Jersey 将名称添加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14103979/