如何使用OpenFeign获取pojo数组? [英] How to use OpenFeign to get a pojo array?

查看:203
本文介绍了如何使用OpenFeign获取pojo数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用OpenFeign客户端访问API,获取一些JSON,然后将其转换为POJO数组.

以前,我只是简单地获取一个JSON字符串,并使用Gson像这样将其转换为数组

FeignInterface {
    String get(Request req);
}
String json = feignClient.get(request);
POJO[] pojoArray = new Gson().fromJson(json, POJO[].class);

这是有效的.我想消除多余的步骤,并假装自动解码JSON并直接直接返回POJO,所以我正在尝试这样做

FeignInterface {
    POJO[] get(Request req);
}
POJO[] pojoArray = feignClient.getJsonPojo(request);`

我遇到了这个错误

feign.codec.DecodeException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 2 path $

两种方法都使用相同的构建器

feignClient = Feign.builder()
     .encoder(new GsonEncoder())
     .decoder(new GsonDecoder())
     .target(FeignInterface.class, apiUrl);

有人有什么主意吗?

解决方案

您破坏了JSON有效负载.序列化之前,您需要删除所有不支持的字符. Feign 允许以下操作:

如果您需要先对响应进行预处理,然后再将其提供给解码器, 您可以使用mapAndDecode构建器方法.一个示例用例是 处理仅提供jsonp的API,您可能需要 在将jsonp发送到您选择的Json解码器之前,先对其进行解包:

public class Example {
  public static void main(String[] args) {
    JsonpApi jsonpApi = Feign.builder()
         .mapAndDecode((response, type) -> jsopUnwrap(response, type), new GsonDecoder())
         .target(FeignInterface.class, apiUrl);
  }
}

因此,您需要在配置中执行以下操作:

  • trim响应,并删除有效负载开头和结尾的所有whitespaces.
  • 删除所有new_line个字符,例如:\r\n\r\n

使用在线工具来确保您的JSON有效负载有效并可以进行反序列化./p>

I’m trying to use a OpenFeign client to hit an API, get some JSON, and convert it to a POJO array.

Previously I was simply getting a string of JSON and using Gson to convert it to the array like so

FeignInterface {
    String get(Request req);
}
String json = feignClient.get(request);
POJO[] pojoArray = new Gson().fromJson(json, POJO[].class);

This was working. I would like to eliminate the extra step and have feign auto decode the JSON and return a POJO directly though, so I am trying this

FeignInterface {
    POJO[] get(Request req);
}
POJO[] pojoArray = feignClient.getJsonPojo(request);`

I am running into this error

feign.codec.DecodeException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 2 path $

Both methods used the same builder

feignClient = Feign.builder()
     .encoder(new GsonEncoder())
     .decoder(new GsonDecoder())
     .target(FeignInterface.class, apiUrl);

Anyone have any ideas?

解决方案

You have broken JSON payload. Before serialising you need to remove all unsupported characters. Feign allows this:

If you need to pre-process the response before give it to the Decoder, you can use the mapAndDecode builder method. An example use case is dealing with an API that only serves jsonp, you will maybe need to unwrap the jsonp before send it to the Json decoder of your choice:

public class Example {
  public static void main(String[] args) {
    JsonpApi jsonpApi = Feign.builder()
         .mapAndDecode((response, type) -> jsopUnwrap(response, type), new GsonDecoder())
         .target(FeignInterface.class, apiUrl);
  }
}

So, you need to do the same in your configuration and:

  • trim response and remove all whitespaces at the beginning and end of payload.
  • remove all new_line characters like: \r\n, \r, \n

Use online tool to be sure your JSON payload is valid and ready to be deserialised .

这篇关于如何使用OpenFeign获取pojo数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆