If you compare your original method to your newly made one (with a List
), you'll notice a few differences.
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. However you're not consuming anything, so you should leave away the 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…