spring - 将列表作为 url 参数发送时返回实体列表
问题描述
如果有人关注并帮助我解决这个问题,我会很高兴。
休息服务器:
@RestController
@RequestMapping("/")
public class RestServer {
@RequestMapping(value = "/foo/{bar}", method=RequestMethod.GET)
@ResponseBody
public List<UserEntity> getUsers(@PathVariable("bar") List<Object> criterions){
return UserService.getInstance().getUserList(criterions);
}
}
休息客户端方法:
public static void initUserMap(){
List<Object> criterions = new ArrayList<>();
criterions.add(Restrictions.eq("UserTypeId", 1));
final String url = "http://localhost:8080/getUsers/" + criterions;
RestTemplate restTemplate = new RestTemplate();
List<UserEntity> users = restTemplate.getForObject(url, UserEntity.class);
..........
..........
}
由于我得到的错误无法编译:
Error:(71, 62) java: incompatible types: inference variable T has incompatible bounds
equality constraints: testRest.UserEntity upper bounds: java.util.List<testRest.UserEntity>,java.lang.Object
如何将客户端方法中的标准作为参数传递给服务器中的 getUsers 方法,以便获得结果,这也是用户列表?
提前致谢。
解决方案
在您的休息客户端中,您指定响应将是 UserEntity 类型,但您声明了一个 List 类型的变量来保存响应。将响应类型更改为 List.class
List<UserEntity> users = restTemplate.getForObject(url, List.class);
推荐阅读
- html - 调整网页大小时如何防止我的 html 元素改变位置?
- typescript - 方法声明参数的自定义 ESLint/TypeScript 缩进规则
- python-3.x - 使用 numpy 确定两个矩阵之间的距离
- python - 如何将 X 定义为列值?
- python - 如何制作statsmodels vector Autoregression可以识别的年份索引?
- javascript - 反应 useState 钩子并以多步形式提交
- python - 更改不和谐机器人昵称 (discord.py)
- ios - 在支持 iOS 13.0 的同时在 iOS 14.0 中使用 @StateObject
- php - 在laravel中输入文本时如何附加?
- css - 如何使用 CSS 仅显示部分字符串