Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 泽西岛可生产列表<;T>;但无法响应。是否确定(List<;T>;).build()?_Java_Json_Jaxb_Jersey_Generic List - Fatal编程技术网

Java 泽西岛可生产列表<;T>;但无法响应。是否确定(List<;T>;).build()?

Java 泽西岛可生产列表<;T>;但无法响应。是否确定(List<;T>;).build()?,java,json,jaxb,jersey,generic-list,Java,Json,Jaxb,Jersey,Generic List,泽西1.6可以生产: @Path("/stock") public class StockResource { @GET @Produces(MediaType.APPLICATION_JSON) public List<Stock> get() { Stock stock = new Stock(); stock.setQuantity(3); return Lists.newArrayList(stock);

泽西1.6可以生产:

@Path("/stock")
public class StockResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Stock> get() {
        Stock stock = new Stock();
        stock.setQuantity(3);
        return Lists.newArrayList(stock);
    }
}
给出错误:
未找到Java类Java.util.ArrayList、Java类型类Java.util.ArrayList和MIME媒体类型application/json的消息正文编写器


这将阻止使用HTTP状态代码和标头。

可以通过以下方式在响应中嵌入
列表

@Path("/stock")
public class StockResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response get() {
        Stock stock = new Stock();
        stock.setQuantity(3);

        GenericEntity<List<Stock>> entity = 
            new GenericEntity<List<Stock>>(Lists.newArrayList(stock)) {};
        return Response.ok(entity).build();
    }
}
@Path(“/stock”)
公共类股票资源{
@得到
@产生(MediaType.APPLICATION_JSON)
公众反应{
股票=新股票();
存货.设定数量(3);
通用实体实体=
新的通用实体(Lists.newArrayList(stock)){};
返回Response.ok(entity.build();
}
}
客户端必须使用以下行获取
列表

public List getStockList(){
WebResource=Client.create().resource(server.uri());
ClientResponse ClientResponse=
资源路径(“股票”)
.type(MediaType.APPLICATION_JSON)
.get(ClientResponse.class);
return clientResponse.getEntity(新的GenericType(){
});
}

由于某种原因,我的GenericType修复程序无法正常工作。但是,由于类型擦除是为集合而不是为数组执行的,所以这是有效的

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Response getEvents(){
        List<Event> events = eventService.getAll();
        return Response.ok(events.toArray(new Event[events.size()])).build();
    }
@GET
@生成(MediaType.APPLICATION\u XML)
公共响应getEvents(){
List events=eventService.getAll();
返回Response.ok(events.toArray(新事件[events.size()])).build();
}

使用异步响应的方法的我的解决方案

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void list(@Suspended
        final AsyncResponse asyncResponse) {
    asyncResponse.setTimeout(10, TimeUnit.SECONDS);
    executorService.submit(() -> {
        List<Product> res = super.listProducts();
        Product[] arr = res.toArray(new Product[res.size()]);
        asyncResponse.resume(arr);
    });
}
@GET
@产生({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
公共作废列表(@已暂停)
最终异步响应(异步响应){
asyncResponse.setTimeout(10,TimeUnit.SECONDS);
executorService.submit(()->{
List res=super.listProducts();
产品[]arr=res.toArray(新产品[res.size()]);
异步响应。恢复(arr);
});
}

此解决方案中有一个小问题是您需要的guava@Necronet:上述服务器端解决方案不需要Guava,对吗?@Nitax
列表。newArrayList
来自Guava。您可以很容易地用纯Java中的
newarraylist
add
替换它。这是最好的解决方案[在此处输入链接说明][1][1]:
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Response getEvents(){
        List<Event> events = eventService.getAll();
        return Response.ok(events.toArray(new Event[events.size()])).build();
    }
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void list(@Suspended
        final AsyncResponse asyncResponse) {
    asyncResponse.setTimeout(10, TimeUnit.SECONDS);
    executorService.submit(() -> {
        List<Product> res = super.listProducts();
        Product[] arr = res.toArray(new Product[res.size()]);
        asyncResponse.resume(arr);
    });
}