目录结构
image.png
配置端口
image.png
创建对象类
package com.example.demo.Hello;
public class User {
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Java List 操作
package com.example.demo.Hello;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class HelloController {
@RequestMapping(value = "/say", method = RequestMethod.GET)
public String say() {
return "hello world";
}
@RequestMapping(value = "/users", method = RequestMethod.GET)
public List<User> getUsers() {
List<User> users = new ArrayList<>();
User userA = new User();
userA.setName("zq");
userA.setAddress("wuhan");
User userB = new User();
userB.setName("zq2");
userB.setAddress("wuhan2");
User userC = new User();
userC.setName("zq3");
userC.setAddress("wuhan3");
User userD = new User();
userD.setName("zq4");
userD.setAddress("wuhan4");
users.add(userA);
users.add(userB);
users.add(userC);
users.add(userD);
return users;
}
}
访问地址
http://localhost:8888/users
网友评论