【问题标题】:Return entity list when sending list as url parameter将列表作为 url 参数发送时返回实体列表
【发布时间】:2019-01-07 20:37:52
【问题描述】:

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

休息服务器:

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

提前致谢。

【问题讨论】:

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


【解决方案1】:

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

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

【讨论】:

  • 我明白了:16:16:59.786 [main] DEBUG org.springframework.web.client.RestTemplate - 为“localhost:8080/getUsers/%5BUserTypeId=1%5D”创建 GET 请求 16:16:59.821 [main] DEBUG org.springframework.web.client.RestTemplate - 将请求接受标头设置为 [application/json, application/*+json] 16:17:03.152 [main] DEBUG org.springframework.web.client.RestTemplate - GET 请求“@ 987654322@" 导致 500(空);调用错误处理程序 org.springframework.web.client.HttpServerErrorException: 500 null
  • 答案是您发布的编译错误,现在您的问题是您不能将列表作为路径变量传递,您需要将条件列表作为查询参数传递跨度>
猜你喜欢
  • 1970-01-01
  • 2012-09-25
  • 1970-01-01
  • 2021-03-30
  • 2018-11-05
  • 2014-06-23
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多