feign.Util.emptyValueOf()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(2.5k)|赞(0)|评价(0)|浏览(199)

本文整理了Java中feign.Util.emptyValueOf()方法的一些代码示例,展示了Util.emptyValueOf()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Util.emptyValueOf()方法的具体详情如下:
包路径:feign.Util
类名称:Util
方法名:emptyValueOf

Util.emptyValueOf介绍

[英]This returns well known empty values for well-known java types. This returns null for types not in the following list.

  • [Bb]oolean
  • byte[]
  • Collection
  • Iterator
  • List
  • Map
  • Set

When Feign.Builder#decode404(), you'll need to teach decoders a default empty value for a type. This method cheaply supports typical types by only looking at the raw type (vs type hierarchy). Decorate for sophistication.
[中]这将返回已知java类型的已知空值。对于不在以下列表中的类型,这将返回null。
*[Bb]oolean
*字节[]
*收藏
*迭代器
*名单
*地图
*设定
假装的时候。在Builder#decode404()中,您需要将类型的默认空值教给解码器。这种方法只需查看原始类型(vs类型层次结构),就可以廉价地支持典型类型。装饰得更精致。

代码示例

代码示例来源:origin: com.netflix.feign/feign-jackson

  1. @Override
  2. public Object decode(Response response, Type type) throws IOException {
  3. if (response.status() == 404) return Util.emptyValueOf(type);
  4. if (response.body() == null) return null;
  5. Reader reader = response.body().asReader();
  6. if (!reader.markSupported()) {
  7. reader = new BufferedReader(reader, 1);
  8. }
  9. try {
  10. // Read the first byte to see if we have any data
  11. reader.mark(1);
  12. if (reader.read() == -1) {
  13. return null; // Eagerly returning null avoids "No content to map due to end-of-input"
  14. }
  15. reader.reset();
  16. return mapper.readValue(reader, mapper.constructType(type));
  17. } catch (RuntimeJsonMappingException e) {
  18. if (e.getCause() != null && e.getCause() instanceof IOException) {
  19. throw IOException.class.cast(e.getCause());
  20. }
  21. throw e;
  22. }
  23. }
  24. }

代码示例来源:origin: com.netflix.feign/feign-core

  1. @Override
  2. public Object decode(Response response, Type type) throws IOException {
  3. if (response.status() == 404) return Util.emptyValueOf(type);
  4. if (response.body() == null) return null;
  5. if (byte[].class.equals(type)) {
  6. return Util.toByteArray(response.body().asInputStream());
  7. }
  8. return super.decode(response, type);
  9. }
  10. }

代码示例来源:origin: io.github.openfeign/feign-jaxb

  1. @Override
  2. public Object decode(Response response, Type type) throws IOException {
  3. if (response.status() == 404 || response.status() == 204)
  4. return Util.emptyValueOf(type);
  5. if (response.body() == null)
  6. return null;

代码示例来源:origin: io.github.openfeign/feign-gson

  1. @Override
  2. public Object decode(Response response, Type type) throws IOException {
  3. if (response.status() == 404)
  4. return Util.emptyValueOf(type);
  5. if (response.body() == null)
  6. return null;
  7. Reader reader = response.body().asReader();
  8. try {
  9. return gson.fromJson(reader, type);
  10. } catch (JsonIOException e) {
  11. if (e.getCause() != null && e.getCause() instanceof IOException) {
  12. throw IOException.class.cast(e.getCause());
  13. }
  14. throw e;
  15. } finally {
  16. ensureClosed(reader);
  17. }
  18. }
  19. }

相关文章