Java 返回 ResponseEntity<List> 返回的 List<myObj>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25355232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:11:07  来源:igfitidea点击:

Return the List<myObj> returned by ResponseEntity<List>

javarestspring-mvcrestful-architectureresttemplate

提问by ND_27

My REST client uses RestTemplate to obtain a List of objects.

我的 REST 客户端使用 RestTemplate 来获取对象列表。

ResponseEntitiy<List> res = restTemplate.postForEntity(getUrl(), myDTO, List.class);

Now I want to use the list returned and return it as List to the calling class. In case of string, toString could be used, but what is the work around for lists?

现在我想使用返回的列表并将其作为 List 返回给调用类。在字符串的情况下,可以使用 toString,但是列表的解决方法是什么?

采纳答案by Christophe L

First off, if you know the type of elements in your List, you may want to use the ParameterizedTypeReferenceclass like so.

首先,如果您知道 List 中元素的类型,您可能希望ParameterizedTypeReference像这样使用该类。

ResponseEntity<List<MyObj>> res = restTemplate.postForEntity(getUrl(), myDTO, new ParameterizedTypeReference<List<MyObj>>() {});

Then if you just want to return the list you can do:

然后,如果您只想返回列表,您可以执行以下操作:

return res.getBody();

And if all you care about is the list, you can just do:

如果你只关心列表,你可以这样做:

// postForEntity returns a ResponseEntity, postForObject returns the body directly.
return restTemplate.postForObject(getUrl(), myDTO, new ParameterizedTypeReference<List<MyObj>>() {});

回答by kaybee99

I couldn't get the accepted answer to work. It seems postForEntityno longer has this method signature. I had to use restTemplate.exchange()instead:

我无法得到公认的工作答案。似乎postForEntity不再有这个方法签名。我不得不restTemplate.exchange()改用:

ResponseEntity<List<MyObj>> res = restTemplate.exchange(getUrl(), HttpMethod.POST, myDTO, new ParameterizedTypeReference<List<MyObj>>() {});

Then to return the list, as above:

然后返回列表,如上:

return res.getBody();

回答by RikudouSennin

In the latest version (Spring Framework 5.1.6) both the answers are not working. As kaybee99 mentioned in his answerpostForEntitymethod signature got changed. Also the restTemplate.exchange()method and its overloads need a RequestEntity<T>or its parent HttpEntity<T>object. Unable to pass my DTO object as mentioned.

在最新版本(Spring Framework 5.1.6)中,这两个答案都不起作用。正如 kaybee99 在他的回答postForEntity方法中提到的那样签名被改变了。此外,该restTemplate.exchange()方法及其重载需要 aRequestEntity<T>或其父HttpEntity<T>对象。如上所述无法传递我的 DTO 对象。

Checkout the documentation of the RestTemplate class

查看 RestTemplate 类的文档

Here is the code which worked for me

这是对我有用的代码

List<Shinobi> shinobis = new ArrayList<>();
shinobis.add(new Shinobi(1, "Naruto", "Uzumaki"));
shinobis.add(new Shinobi(2, "Sasuke", "Uchiha");
RequestEntity<List<Shinobi>> request = RequestEntity
            .post(new URI(getUrl()))
            .accept(MediaType.APPLICATION_JSON)
            .contentType(MediaType.APPLICATION_JSON)
            .body(shinobis);
ResponseEntity<List<Shinobi>> response = restTemplate.exchange(
            getUrl(), 
            HttpMethod.POST, 
            request, 
            new ParameterizedTypeReference<List<Shinobi>>() {}
            );
List<Shinobi> result = response.getBody();

Hope it helps someone.

希望它可以帮助某人。

回答by kris

You have unwrap the ResponseEntity and you can get the object(list)

你已经解开 ResponseEntity 并且你可以得到对象(列表)

 res.getBody()

回答by kris

You can use the ParameterizedTypeReferenceclass of Spring to convert to List the data returned by ResponseEntity

可以使用Spring的ParameterizedTypeReference类将ResponseEntity返回的数据转换为List

ResponseEntity<List<MyObject>>  resp = restTemplate.exchange(url, HttpMethod.GET, null, new ParameterizedTypeReference<List<MyObject>>(){});

if(resp != null && resp.hasBody()){
   List<MyObject> myList = resp.getBody();
}