3 使用httpclient对象实现RPC-返回对象类型

2023-02-05 14:29:27
  1. 实体对象:
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 + '\'' +
                '}';
    }
}
  1. 服务端代码:
@RequestMapping("getUserByRemote")
    public User getUserByRemote(User user){
        return user;
    }
  1. httpclient端代码:
/**
     * 获取json对象响应格式
     */
    @Test
    public void testHttpPost2(){
        try {
            //1.构建http对象(相当于浏览器)
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //3.创建http post请求对象
            HttpPost post = new HttpPost("http://localhost:18081/testBoot/getUserByRemote");
            /**
             * 3.1添加请求参数,post请求参数是在body中
             * NameValuePair:封装参数的接口
             * BasicNameValuePair: 接口实现类
             */
            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 httpEntity = new UrlEncodedFormEntity(params,"utf-8");
            post.setEntity(httpEntity);//设置参数体

            //4.创建响应对象
            CloseableHttpResponse response = httpClient.execute(post);
            /**
             * 由于响应接口返回的是一个字符串,因此需要转换
             * 响应体数据封装在HttpEntity对象中
             */
            String s = EntityUtils.toString(response.getEntity(), "utf-8");
            System.out.println(s);

            //5.释放资源
            response.close();
            httpClient.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  1. 响应结果:
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

  • 作者:水无痕simon
  • 原文链接:https://blog.csdn.net/weixin_39563769/article/details/124976385
    更新时间:2023-02-05 14:29:27