Error while extracting response for type [java.util.List<java.lang.String>] and content type [applic

2021年03月23日 18:53 · 阅读(12210) ·

开发环境

名称 版本
操作系统 Windows 10 X64
JDK JDK1.8(jdk-8u151-windows-x64)
IntelliJ IDEA IntelliJ IDEA 2018.3
Maven Maven 3.6.0

问题描述

调用接口报错,前端提示

  1. {
  2. "code": 400,
  3. "message": "DemoConsumer#getUserRole(String) failed and no fallback available."
  4. }

后端代码报错为

  1. Caused by: feign.codec.DecodeException: Error while extracting response for type [java.util.List<java.lang.String>] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
  2. Caused by: org.springframework.web.client.RestClientException: Error while extracting response for type [java.util.List<java.lang.String>] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of
  3. Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
  4. Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
  5. at [Source: (PushbackInputStream); line: 1, column: 1]

问题分析

是调用 getUserRole 接口报的错

先看生产者这个接口的定义

  1. /**
  2. * 获取用户对应的角色权限
  3. *
  4. * @param userId 用户角色权限
  5. * @return List<String>
  6. **/
  7. @PostMapping("/getUserRole")
  8. public List<String> getUserRole(@RequestParam(value = "userId") String userId)

我在 web 项目中调用如下

  1. List<String> roleList = consumer.getUserRole(result.getData().getId());

问题解决

看上去没什么问题,打入断点到上面的代码,发现这里 Id 没有值,导致报错

把 Id 设置有值之后,错误消失,问题解决。