将列表作为url参数发送时返回实体列表

时间:2018-07-31 12:35:18

标签: spring hibernate rest resttemplate spring-boot-starter

如果有人关注并帮助我解决此问题,将会很高兴。

其他服务器:

@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方法,以便得到结果,这也是用户列表?

谢谢。

1 个答案:

答案 0 :(得分:-1)

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

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