- 实体对象:
public class User {
private Integer id;
private String userName;
private String passWord;
private String realName;
public User() {
}
public User(Integer id, String userName, String passWord, String realName) {
this.id = id;
this.userName = userName;
this.passWord = passWord;
this.realName = realName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", realName='" + realName + '\'' +
'}';
}
}
- 服务端代码:
@RequestMapping("getUserByRemote")
public User getUserByRemote(User user){
return user;
}
- httpclient端代码:
@Test
public void testHttpPost2(){
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost("http://localhost:18081/testBoot/getUserByRemote");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("id","33"));
params.add(new BasicNameValuePair("userName","ss"));
params.add(new BasicNameValuePair("passWord","dssds"));
HttpEntity httpEntity = new UrlEncodedFormEntity(params,"utf-8");
post.setEntity(httpEntity);
CloseableHttpResponse response = httpClient.execute(post);
String s = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(s);
response.close();
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
- 响应结果:
23:43:44.145 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {}->http://localhost:18081][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]
{"id":33,"userName":"ss","passWord":"dssds","realName":null}
23:43:44.145 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection manager is shutting down