问题描述
我们正在调用一个返回简单列表的外部 API,而不是 DTO/Json,例如 ResponseEntity<List<String>>
在我的测试中,我尝试使用 mockServer
模拟响应,但未能将结果作为 List 返回(只是 String,因为它对我没有帮助):
mockServer.when(restTemplate.getForObject(any(),any()))
.respond(HttpResponse.response("{\"40\",\"50\"}")
.withStatusCode(200)
.withDelay(TimeUnit.SECONDS,1));
String user = "user";
String password = "password";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(user,password));
List<String> retList = restTemplate.getForObject(URI,List.class);
我尝试使用 [
、]
、{
、}
,它们都没有,双括号但未能将响应作为列表返回..
很高兴知道正确执行此操作,谢谢!
附言在代码中,所有与真正的 API 都运行良好,响应也作为列表出现。问题仅在测试中,错误是:
"无法提取响应:找不到合适的 HttpMessageConverter 对于响应类型 [接口 java.util.List] 和内容类型 [应用程序/八位字节流]"
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)