实体类
import lombok.Data;
@Data
public class Animal {
private String name;
private Integer age;
}
1 返回toString
方法的返回值是String类型
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping(value ="/test")
public class SpringBootTest {
@GetMapping(value ="/hello")
public @ResponseBody
String hello() {
List list = new ArrayList();
Animal animal=new Animal();
animal.setName("xx");
animal.setAge(10);
list.add(animal);
return animal.toString();
}
}
2 返回list
方法的返回值是List类型
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping(value ="/test")
public class SpringBootTest {
@GetMapping(value ="/hello")
public @ResponseBody
List hello() {
List list = new ArrayList();
Animal animal=new Animal();
animal.setName("xx");
animal.setAge(10);
list.add(animal);
Animal animal1=new Animal();
animal1.setName("xx");
animal1.setAge(10);
list.add(animal1);
return list;
}
}
返回的List其实是数组