Java 如何使用 Spring Boot 返回一组对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41719142/
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
How to return a set of objects with Spring Boot?
提问by faoxis
I did a lessonabout Spring Boot
and it works perfectly. But what if I want to return a set of objects ? I tried doing thisbut it doesn't work. How can I do it correctly ?
我做了一个关于Spring Boot
它的课程,它完美地工作。但是如果我想返回一组对象呢?我试着做这个,但它不工作。我怎样才能正确地做到这一点?
With one object (it works):
使用一个对象(它有效):
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
With many objects (it doesn't work):
有很多对象(它不起作用):
@RequestMapping(value = "/greeting", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Greeting> greeting() {
Greeting greeting1 = new Greeting(1, "One");
Greeting greeting2 = new Greeting(2, "Two");
List<Greeting> list = new ArrayList<>();
list.add(greeting1);
list.add(greeting2);
return list;
}
采纳答案by g00glen00b
If you compare your original method to your newly made one (with a List
), you'll notice a few differences.
如果您将原始方法与新制作的方法(带有List
)进行比较,您会发现一些不同之处。
First of all, within the @RequestMapping
annotation you're now using the properties consumes
and produces
. produces
is not a problem here, because you are producing a response that should be JSON. Howeveryou're not consuming anything, so you should leave away the consumes
.
首先,在@RequestMapping
注释中,您现在正在使用属性consumes
和produces
。produces
在这里不是问题,因为您正在生成一个应该是 JSON 的响应。但是你没有消耗任何东西,所以你应该离开consumes
.
@RequestMapping(value = "/greeting", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
List<Greeting> greeting() {
Greeting greeting1 = new Greeting(1, "One");
Greeting greeting2 = new Greeting(2, "Two");
List<Greeting> list = new ArrayList<>();
list.add(greeting1);
list.add(greeting2);
return list;
}
As a sidenote, you might also notice that you used the @ResponseBody
annotation. Putting it here won't cause any errors, but it is not necessary, because if you followed the Spring tutorial correctly, you should have annotated your controller with @RestController
and by doing that, you already tell Spring that it will use a response body.
作为旁注,您可能还会注意到您使用了@ResponseBody
注释。把它放在这里不会导致任何错误,但它不是必需的,因为如果你正确地遵循了 Spring 教程,你应该已经注释了你的控制器,@RestController
通过这样做,你已经告诉 Spring 它将使用响应主体。
回答by johnpili
Here is the code snippet I did for this. Remove the "consume" from your @RequestMapping annotation because you are not using that in your method.
这是我为此所做的代码片段。从 @RequestMapping 注释中删除“consume”,因为您没有在方法中使用它。
@RestController
public class GreetingsController
{
@RequestMapping(value = "greetings", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
List<Greeting> greeting() {
Greeting greeting1 = new Greeting(1, "One");
Greeting greeting2 = new Greeting(2, "Two");
List<Greeting> list = new ArrayList<>();
list.add(greeting1);
list.add(greeting2);
return list;
}
public class Greeting
{
private String message;
private int count;
public Greeting(int count, String message)
{
this.count = count;
this.message = message;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
}
}
回答by Vipul Gulhane
Let Say we have list of CarDetails Pojo and we want to return them back
假设我们有 CarDetails Pojo 列表,我们想将它们返回
@RestController
public class CarDetailController {
@GetMapping("/viewAllCarDetailList")
public List<CarDetail> retrieveAllCarDetails() {
List<CarDetail> contacts = new ArrayList<CarDetail>();
CarDetail objt = new CarDetail();
objt.setCarModel("hyundai");
objt.setSubModel("I10");
CarDetail objt2 = new CarDetail();
objt2.setCarModel("hyundai");
objt2.setSubModel("I20");
contacts.add(objt);
contacts.add(objt2);
return contacts;
}
}
public class CarDetails {
private String carModel;
private String subModel;
// Will haave Setter getter and hash code equls method
//and constructor
}
This JSON will be output:-
此 JSON 将被输出:-
[
{
"carModel": "hyundai",
"subModel": "I10"
},
{
"carModel": "hyundai",
"subModel": "I20"
}
]