Gb1400RestTemplate.java 1.81 KB
package com.viontech.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @author vion
 **/
@Component
@Slf4j
public class Gb1400RestTemplate {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private Gb1400Config gb1400Config;

    public String doPost(String userIdentify, String uri, String content) {
        HttpHeaders httpHeaders = setHeader(userIdentify);
        if (!uri.startsWith("/")) {
            uri = "/" + uri;
        }
        String url = "http://" + gb1400Config.getIpPort() + uri;
        HttpEntity<String> entity = new HttpEntity<>(content, httpHeaders);
        if (content.length() > 500) {
//            log.info("1400:{} data docking,params:{}", uri, content.substring(0, 500));
            log.info("1400:{} data docking,params:{}", uri, content);
        } else {
            log.info("1400:{} data docking,params:{}", uri, content);
        }
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class);
        String result = responseEntity.getBody();
        log.info("1400:{} data docking,result:{}", uri, result);
        return result;
    }

    private HttpHeaders setHeader(String userIdentify) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("User-Identify", userIdentify);
        headers.add("Content-Type", "application/VIID+JSON;charset=utf-8");
//        String currentTime = DateUtil.getCurrentTime(DateUtil.TIMESTAMP_NO_DELIMITER);
//        headers.add("User-SendTime", currentTime);
        return headers;
    }
}