在我所认知的get方法一般都是在属性被设置成只读的时候所需要使用的,一般都默认需要但是不知道其他用处,然后测试的时候我就没去写get方法,导致了如下错误:
我的User类
public class User {
private Integer uid;
private String username;
private String password;
private Integer age;
public User(){
}
public User(Integer uid,String username,String password,Integer age){
this.uid = uid;
this.username = username;
this.password = password;
this.age = age;
}
我的模拟数据
@GetMapping("/findAll")
public BaseResult findAll(){
//模拟所有数据
ArrayList<User> list = new ArrayList<>();
list.add(new User(1,"张三","1111",12));
list.add(new User(2,"李四","2222",15));
list.add(new User(3,"王五","3333",10));
list.add(new User(4,"赵六","4444",18));
//封装一个返回数据
return BaseResult.ok("查询成功",list);
}
我的测试内容
@Test
public void findAll(){
//创建客户端
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
//创建get请求
HttpGet httpGet = new HttpGet("http://localhost:9090/user/findAll");
try {
//发送请求获取响应
response = httpClient.execute(httpGet);
//获取状态码
if (response.getStatusLine().getStatusCode() == 200){
//获取响应
String s = EntityUtils.toString(response.getEntity());
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//释放资源
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
一开始我认为这是一个没有什么问题的测试,结果首先给我报了两个错误:
前面太多了,这里是因为序列化接口没实现
这个过,在我实现了序列化接口之后