com.fasterxml.jackson.databind.JsonMappingException(通过引用链:java.util.ArrayList [0])

时间:2017-01-11 18:56:04

标签: java json rest jackson fasterxml

我们正在为wildfly部署休息服务。如下调用其余服务是成功的。

Entity<List<Product>> entity = Entity.entity(productList,MediaType.APPLICATION_JSON);
Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.method("POST", entity);

休息服务定义

@Path("/streams/productList")
public class ProductListService extends ServiceBase {

  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response sendProducts(@Valid List<Product> pdts) throws IOException, URISyntaxException {
    // more customization needed
    return sendResponse(200, CommonUtils.getIds(pdts), new ArrayList<String>());
  }
}

新要求是将json有效负载发送到其余服务,该服务将转换为产品列表。 下面不起作用!这是每个产品都是一个字符串json有效负载添加到列表中。

List<String> productsAsJsonPayload = retrievePayloadFromSomewhere();
Entity<List<String>> entity = Entity.entity(productsAsJsonPayload,MediaType.APPLICATION_JSON);
Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.method("POST", entity);

这给出了错误:

com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.abc.enterprise.devsvc.model.Product] from String value ('{"id":"00000001-0001-0001-0001-000000000001","type":"Some Product","serial":"MR-1234567","messageTimestamp":1484110968720,....}'); no single-String constructor/factory method
 at [Source: io.undertow.servlet.spec.ServletInputStreamImpl@207f12a8; line: 1, column: 2] (through reference chain: java.util.ArrayList[0])

我必须进行这些更改才能使其正常工作:(基本上添加[])

  StringBuilder jsonPayloadAsArray = new StringBuilder("[");
  for (int idx = 0; idx < productsAsJsonPayload.size(); idx++) {
    jsonPayloadAsArray.append(productsAsJsonPayload.get(idx));
    if (idx < productsAsJsonPayload.size()-1) {
      jsonPayloadAsArray.append(",");
    }
  }
  jsonPayloadAsArray.append("]");

Entity<String> entity = Entity.entity(jsonPayloadAsArray.toString(),MediaType.APPLICATION_JSON);
Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.method(httpMethod, entity);

但这是一个黑客。有没有办法绕过这个黑客?

0 个答案:

没有答案