【问题标题】:How to parse REST API STREAM如何解析 REST API 流
【发布时间】:2020-03-28 14:58:14
【问题描述】:

我正在向服务器发送一个 get 请求,服务器返回以下两个响应。当事件在流中(如 id1、id2、id3、id4 ......等)而不是一次发生在服务器上时,会收到这些响应。

现在,我需要一一获取这些响应并对其进行解析,然后将其保存为对象以供进一步使用。

我如何实现这个 java 8 和 spring MVC?

id: 1
data: {"event_type":"ABC","business_call_type":"XYZ","agent_number":"nnn","call_recording":null,"number":"0000","uuid":"a","call_direction":"Outbound","caller":"+100000000000","customer_number":"+100000000000","version":"1.0","k_number":"+917303454203","type":"AGENT_CALL","unique_id":"0","call_solution":"xx","FreeSWITCH_IPv4":"11111","Event_Date_Local":"2020-03-28 11:46:47"}

id: 2
data: {"event_type":"AGENT_ANSWER","business_call_type":"Outbound","agent_number":"+1111111111","call_recording":null,"number":"+22222222","uuid":"bbbbbbbbbbbbbb","call_direction":"Outbound","caller":"+100000000000","customer_number":"+100000000000","version":"1.0","k_number":"+1111111111","type":"AGENT_ANSWER","unique_id":"bbbbbbbbbb","call_solution":"xx","FreeSWITCH_IPv4":"0.0.0.0","Event_Date_Local":"2020-03-28 11:47:00"}

【问题讨论】:

  • 看看 RestTemplate baeldung.com/rest-template
  • 嗨,大卫,这本质上是异步的,并且在 baground 中运行。请通过以下代码找到我为获取响应主体而编写的代码,但我在记录器中收到的数据中没有得到任何响应。请查看答案中的代码并建议@DavidPi

标签: java spring rest spring-mvc java-8


【解决方案1】:

下面是上面 json 解析使用的代码。

import java.util.HashMap;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.AsyncRestTemplate;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.context.request.async.DeferredResult;

import com.psg.async_tasks.controller;
import com.psg.dao.CtiIntegrationdao;

// @Controller
@Service
public class ListningService {
    private static final Logger logger = Logger.getLogger(ListningService.class.getName());
    @Autowired
    CtiIntegrationdao daoCtiInt;

    //@RequestMapping({"list"})
    @PostConstruct
    public  void   ListningReponse() {
        HashMap<String,String> results=daoCtiInt.getKnolarity_Config();
        String endpoint;
        endpoint=results.get("30");
        endpoint=endpoint.replace("<<AUTH>>",results.get("26"));
        logger.info(endpoint);
        logger.info("============================================================================================@postconstruct=========");
        AsyncRestTemplate asyncrestTemplate = new AsyncRestTemplate();
        try {
             final DeferredResult<String> result = new DeferredResult<>();
               ListenableFuture<ResponseEntity<String>> futureEntity = asyncrestTemplate.getForEntity(endpoint, String.class);
               logger.info("IN TRY");
               logger.info(futureEntity.toString());

               futureEntity.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
            @Override
            public void onSuccess(ResponseEntity<String> result) {
                String[] idno = result.getBody().split("\\R", 3);
                System.out.println("==================="+idno[0]);
                String responseBody =result.getBody().replaceAll("id: (\\d+)","").replace("data: ","");;
                logger.info("-----responsebody-----"+responseBody);
                logger.info("-----responsebody-----"+result.getBody());
//              logger.info("-----responsebody-----"+result.getBody().getAgent_number());
//              logger.info("-----responsebody-----"+result.getBody().getBusiness_call_type());
//              logger.info("-----responsebody-----"+result.getBody().getCall_duration());
//              logger.info("-----responsebody-----"+result.getBody().getCall_recording());
//              logger.info("-----responsebody-----"+result.getBody().getCall_solution());
//              logger.info("-----responsebody-----"+result.getBody().getCall_Type());
//              logger.info("-----responsebody-----"+result.getBody().getDestination());



            }

            @Override
            public void onFailure(Throwable ex) {
                result.setErrorResult(ex.getMessage());
                logger.info("------------Failure Block"+result.toString());

            }
               });
        }catch(HttpClientErrorException ex) {
            logger.info(ex.getMessage());           
        }catch(Exception ex) {
            ex.printStackTrace();
        }

    }
}

【讨论】:

    猜你喜欢
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多