jt-day17

image

1.HttpClient

1.HttpClient 是 Apache Jakarta Common 下的子項目,用來提供高效的、最新的、功能豐富的支持 HTTP 協議的客戶端編程工具包
業務需求說明
image.png前端

  1. HttpClient入門案例
    1)### 導入jar包
    2)### HttpClient入門案例
package com.jt.test;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
//@SpringBootTest   //從spring容器中獲取bean對象,以後完成測試業務.
public class TestHttpClient {
    /**
     * 1.實例化HttpClient對象
     * 2.定義遠程訪問的url地址
     * 3.定義請求類型的對象
     * 4.發起http請求,獲取響應的結果
     * 5.對返回值結果進行校驗.獲取真實的數據信息.
     * */
    @Test
    public void testGet() throws IOException {
       HttpClient httpClient = HttpClients.createDefault();
       String url = "http://www.baidu.com";
       HttpGet httpGet = new HttpGet(url);
       HttpResponse httpResponse = httpClient.execute(httpGet);
       //常見結果狀態 200 404 406參數異常  500後端服務器異常 504超時  502訪問的網址不存在
        //獲取狀態碼
        int status = httpResponse.getStatusLine().getStatusCode();
        if(status == 200){
            //獲取響應結果
            HttpEntity entity = httpResponse.getEntity();
            String result = EntityUtils.toString(entity,"UTF-8");
            System.out.println(result);
        }
    }
}

3.HttpClient實現業務邏輯(前端【controller--service】--後端【controller--service】)
1)### 業務需求
用戶經過http://www.jt.com/user/testHt...
JT-WEB服務器 訪問JT-SSO時的請求http://sso.jt.com/user/testHt...
2)### 編輯前端Controllerjava

@RequestMapping("/testHttpClient")
    @ResponseBody
    public List<User> testHttpClient(){

        return userService.testHttpClient();
    }

3) ### 編輯前端Serviceweb

package com.jt.service;
import com.jt.pojo.User;
import com.jt.util.ObjectMapperUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
    @Override
    public List<User> testHttpClient() {
        List userList = new ArrayList<>();
        //由jt-web服務器去連接jt-sso的服務器
        String url = "http://sso.jt.com/user/testHttpClient";
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse httpResponse =httpClient.execute(httpGet);
            if(httpResponse.getStatusLine().getStatusCode() == 200){
            HttpEntity httpEntity = httpResponse.getEntity();
            String result = EntityUtils.toString(httpEntity, "UTF-8");
            userList = ObjectMapperUtil.toObject(result, userList.getClass());
           /* for (LinkedHashMap<String,Object> map : userList){
                    User userTemp = new User();
                    userTemp.setId( Long.parseLong(map.get("id")+""));
                    userTemp.setUsername((String)map.get("username"));
                    userTemp.setPassword((String)map.get("password"));
                    userTemp.setPhone((String)map.get("phone"));
                    userTemp.setEmail((String)map.get("phone"));
                    userList2.add(userTemp);
                }*/
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return userList;
    }
}

4) ### 編輯後端Controllerspring

/**
     * http://sso.jt.com/user/testHttpClient
     * 返回List<User>
     */
    @RequestMapping("testHttpClient")
    public List<User> testHttpClient(){

        return userService.findAll();
    }

5) ### 編輯後端Serviceapache

@Override
    public List<User> findAll() {

        return userMapper.selectList(null);
    }
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息