首页 > 解决方案 > 将列表作为 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 方法,以便获得结果,这也是用户列表?

提前致谢。

标签: springhibernaterestresttemplatespring-boot-starter

解决方案


在您的休息客户端中,您指定响应将是 UserEntity 类型,但您声明了一个 List 类型的变量来保存响应。将响应类型更改为 List.class

List<UserEntity> users = restTemplate.getForObject(url, List.class);

推荐阅读