java - 无法将类型 [java.lang.String] 的值转换为所需类型 [org.javatuples.Pair] : no matching editors or conversion strategy found

标签 java request

我尝试通过请求 URL 传递 javatuples.Pair 类型并获取异常。无论是否正确传递,都不是用户。

请在下面找到我的代码:

public <T> T getUserToNotifyDeactivationByRole(List<Pair<String, Integer>> rolePair, Class<T> responseType) {
      String href = securityOptions.linkTo("users").concat(format("/notifyUserDeactivationByRole?rolePair=%s", join(rolePair, ',')));
      return restTemplate.getForEntity(href, responseType).getBody();
  }

请查找以下错误日志:

exception : Failed to convert value of type 'java.lang.String' to required type 'java.util.List'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.javatuples.Pair]: no matching editors or conversion strategy found

Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.javatuples.Pair]: no matching editors or conversion strategy found

我在另一个需求中使用了join。 (来自 apache.commons.lang3)

public <T> T getUser(List<String> districtIds, Class<T> responseType) {
    String href = securityOptions.linkTo("users").concat(format("/get-userlist-by-createaccess?districtIds=%s", join(districtIds, ',')));
    return restTemplate.getForEntity(href, responseType).getBody();
  }

最佳答案

getForEntity 方法显然无法将响应转换为 Pair,但是,您可以尝试以 String 形式获取响应,然后手动将 String 转换为 Pair。 Rest 模板不知道如何将获得的响应映射到 Pair。

如果您使用 Jackson 进行反序列化,您可以尝试按照此处的方式添加 mixin: https://github.com/javatuples/javatuples/issues/3#issuecomment-390901512

关于java - 无法将类型 [java.lang.String] 的值转换为所需类型 [org.javatuples.Pair] : no matching editors or conversion strategy found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57306879/

相关文章:

java - Mockito 非 stub 函数

python - Django:过滤请求结果以仅包含与请求用户相关的数据

node.js - Nodejs请求模块返回错误的statusCode

Node.js 使用 Content Disposition 作为文件名下载文件

java - 根据 XSD 验证 XML 顺序的框架

java - 将简单的 JSON 字符串转换为对象

java - 类文件中的内部类但不在类主体中?

java - 有没有一种优雅的方法可以在指定的超时时间内处理多个 future?

AngularJS : How to prevent a request

django - 使用 Django 发出 HTTP 请求并反序列化输出