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);

  

posted @ 2019-12-31 17:13  刘百会  阅读(1689)  评论(0编辑  收藏  举报