java11新特性HttpClient


1、get請求

ObjectMapper objectMapper = new ObjectMapper();
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder().uri(URI.create(softPhoneAddress + "/api/cfg/selectAgentBySkill?skilldbid=" + queueId))
                .GET()
                .build();
        try {
            HttpResponse<String> response =
                    client.send(request, HttpResponse.BodyHandlers.ofString());
            String body = response.body();
            List<SeatAgentsVo> seatAgentsVo = objectMapper.readValue(body, new TypeReference<List<SeatAgentsVo>>() {
            });

  2、post請求

 //objectIds轉換成json字符串
        Objectids object = new Objectids();
        object.setObjectids(objectIds);
        ObjectMapper mapper = new ObjectMapper();
        String result = null;
        try {
            result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        HttpClient builder = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder(URI.create(softPhoneAddress + "/api/v2/stats/agents"))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(result))
                .build();
        try {
            HttpResponse<String> send = builder.send(request, HttpResponse.BodyHandlers.ofString());
            String body = send.body();
            SeatStatus value = mapper.readValue(body, SeatStatus.class);

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM