gpt4 book ai didi

java - 如何通过 REST 传输 ArrayList

转载 作者:行者123 更新时间:2023-12-01 09:05:12 29 4
gpt4 key购买 nike

编辑:我尝试实现@Durgpal Singh 和@Nikhil 的建议。我更改了代码,所以它看起来像这样。

客户:

    Client client = ClientBuilder.newClient();
WebTarget target = client
.target("http://localhost:8087/api/ls3algorithm/" + petrinets + "/" + Integer.toString(k) + "/" + Float.toString(theta));

Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.get();

Map<String, List<Map>> result_ = response.readEntity(new GenericType<Map<String, List<Map>>>() { });
result = (ArrayList<Map>) result_.get("data");

服务器:

ArrayList<Map> result;

result = new Ls3Algorithm().execute(new File("petrinetze").getAbsolutePath(), k, theta);

Map<String, List<Map>> map = new HashMap<>();
map.put("data", result);
return Response.ok(map).build();

不幸的是,这会导致Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=interface java.util.Map, genericType=java.util.Map<java.lang.String, java.util.List<java.util.Map>>.

我哪里出错了?

-----------------------------------------

我对 RESTful Web 服务还很陌生,目前正在编写一个提供计算算法的微服务。我正在测试下面发布的服务。

工作流程:客户端将一些数据保存在 MongoDB 数据库中,并通过 @PathParam 作为 GET 请求的一部分发送相关文件的名称。然后,服务器从 MongoDB 检索文件,处理其算法并将结果发送回 List<Map>包装在 Response对象。

目标:将结果 ( List<Map> ) 作为 JSON 传输并在客户端控制台上打印出来。

客户:

package ls3test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSInputFile;

public class Ls3TransmissionTest {
final static String petrinets = "eins, zwei, drei, vier";
final static int k = 3;
final static float theta = 0.9f;

public static void main(String[] args) throws IOException {

[... save all the relevant files in the MongoDB ...]

ArrayList<Map> result = new ArrayList<Map>();

Client client = ClientBuilder.newClient();
WebTarget target = client
.target("http://localhost:8087/api/ls3algorithm/" + petrinets + "/" + Integer.toString(k) + "/" + Float.toString(theta));

Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.get();

result = response.readEntity(new GenericType<ArrayList<Map>>() {
});

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(result);
}
}

服务器:

package service;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

@SuppressWarnings("deprecation")
@Path("/ls3algorithm")
public class Resource {

// SLF4J is provided with Dropwizard
Logger log = LoggerFactory.getLogger(Resource.class);

@SuppressWarnings("rawtypes")
@GET
@Path("/{petrinets}/{k}/{theta}")
@Produces(MediaType.APPLICATION_JSON)
public Response ls3execute(@PathParam("petrinets") String petrinetNames, @PathParam("k") int k,
@PathParam("theta") float theta) {

[... get all the relevant files from the MongoDB ...]

List<Map> result;
Ls3Algorithm ls3Algorithm = new Ls3Algorithm();

result = ls3Algorithm.execute(new File("petrinetze").getAbsolutePath(), k, theta);

GenericEntity<List<Map>> entity = new GenericEntity<List<Map>>(result) {};
Response response = Response.ok(entity).build();

return response;
}
}

这不起作用,我得到的异常发布如下:

Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.ArrayList<java.util.Map>.
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:231)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1085)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:834)
at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:368)
at org.glassfish.jersey.client.InboundJaxrsResponse$2.call(InboundJaxrsResponse.java:126)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:419)
at org.glassfish.jersey.client.InboundJaxrsResponse.runInScopeIfPossible(InboundJaxrsResponse.java:267)
at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:123)
at ls3test.Ls3TransmissionTest.main(Ls3TransmissionTest.java:89)

Ls3TransmissionTest.java:89ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();

我现在花了很多时间研究这个问题,但我找不到真正适合它的例子。我想念什么?非常感谢任何帮助或提示!

最佳答案

您可以发送 map 。像这样

Map<String, Object> map = new HashMap<>();
map.put("data", entity);
Response.ok(map).build();
return Response;

关于java - 如何通过 REST 传输 ArrayList<Map>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41337170/

29 4 0