我尝试通过请求 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/