RestTemplate
package com.ssdl.advertise.service.impl;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject;
import com.ssdl.advertise.dto.req.SendDaPingReqDto;
import com.ssdl.advertise.service.MessageDaPinService;
import com.ssdl.advertise.util.TimeUtils;
import lombok.extern.log4j.Log4j2;
@Service
@Log4j2
public class MessageDaPinServiceImpl implements MessageDaPinService{
@Value("${kht.daPingUrl}")
private String daPingUrl;
@Autowired
private RestTemplate restTemplate;
@Override
public void sendDaPing(String brandName, String biEndTime, String imageUrl, String licenseNo) {
SendDaPingReqDto sendDaPingRequest = new SendDaPingReqDto();
sendDaPingRequest.setCity("上海市");
sendDaPingRequest.setStoreName("海宝养车");
sendDaPingRequest.setBrandName(brandName);
sendDaPingRequest.setBiEndTime(biEndTime);
sendDaPingRequest.setScanTime(TimeUtils.format(new Date(), TimeUtils.yyyy_MM_dd_HH_mm_ss));
sendDaPingRequest.setImageUrl(imageUrl);
sendDaPingRequest.setLicenseNo(licenseNo);
log.info("推送到大屏的请求参数:" + sendDaPingRequest);
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/json; charset=UTF-8"));
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
HttpEntity<String> formEntity = new HttpEntity<String>(JSONObject.toJSONString(sendDaPingRequest), headers);
ResponseEntity<String> res = restTemplate.postForEntity(daPingUrl, formEntity, String.class);
log.info("推送到大屏的返回参数:" + res.getBody());
} catch (Exception e) {
log.error("推送到大屏报错:", e);
e.printStackTrace();
}
}
}