1.首先启动服务提供方工程的引导类,使浏览器输入url: http://localhost:8081/user/1时可以正常访问controller,并得到返回的结果:
2.新建一个工程项目,并将httpcomponents的依赖导入:
3.创建User实例对象,与反序列化后的对象相对应:
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String username;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUserame(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
4.在test目录下的测试类中引入反序列化的方法:
private static final ObjectMapper MAPPER = new ObjectMapper();
5.启动该测试方法:
@Test
public void testGetPojo() throws IOException {
HttpGet request = new HttpGet("http://localhost:8081/user/1");
String response = this.httpClient.execute(request, new BasicResponseHandler());
System.out.println(response);
//调用方法反序列化并
User user= MAPPER.readValue(response,User.class);
System.out.println(user);
}
6.得到结果为User对象: