RestTemplate 组装数据

@Component
public class CouponOnlineTask {
/*
@Autowired
private CouponOnline couponOnline;
private RestTemplate restTemplate;*/
@Value(“ a p i . o c m . c o u p o n O n l i n e U r l " ) p r i v a t e S t r i n g c o u p o n U r l ; @ V a l u e ( " {api.ocm.couponOnlineUrl}") private String couponUrl; @Value(" api.ocm.couponOnlineUrl")privateStringcouponUrl;@Value("{api.ocm.couponCancellationUrl}”)
private String couponCancellationUrl;
@Value(“ o c m . u s e r N a m e " ) p r i v a t e S t r i n g o c m U s e r N a m e ; @ V a l u e ( " {ocm.userName}") private String ocmUserName; @Value(" ocm.userName")privateStringocmUserName;@Value("{ocm.secretKey}”)
private String ocmSecretKey;

private static final Logger LOGGER =  LoggerFactory.getLogger(CouponOnlineTask.class);

@XxlJob(“couponIssueTask”)
public void couponIssueTask(){
LOGGER.info(“定时任务开始执行,批次发放优惠卷…”);
EEyeLogSender.info(“定时任务开始执行,批次发放优惠卷…”);
LOGGER.info(“url”+couponUrl+" username:"+ocmUserName+“ocmSeretKey”+ocmSecretKey);
// 调用定时任务
try{
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType(“application/json; charset=UTF-8”);
headers.setContentType(type);
headers.add(“Accept”, MediaType.APPLICATION_JSON.toString());
headers.add(“userName”, ocmUserName);
headers.add(“secretKey”,ocmSecretKey);
//JSONObject jsonObj = JSONObject.fromObject(params);

        HttpEntity formEntity = new HttpEntity(headers);

        String result = restTemplate.postForObject(couponUrl, formEntity, String.class);

        LOGGER.info("执行优惠券批次发放定时任务返回"+result.toString());
        EEyeLogSender.info("执行优惠券批次发放定时任务返回"+result.toString());
    }catch (Exception e){
        e.printStackTrace();
        LOGGER.error("定时任务发放优惠券出错.",e);
        EEyeLogSender.error("定时任务发放优惠券出错"+e);
    }

}

@XxlJob(“couponIssueTaskFri”)
public void couponIssueTaskFri(){
LOGGER.info(“定时任务开始执行,批次发放优惠卷…”);
EEyeLogSender.info(“定时任务开始执行,批次发放优惠卷…”);
LOGGER.info(“url”+couponUrl+" username:"+ocmUserName+“ocmSeretKey”+ocmSecretKey);
// 调用定时任务
try{
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType(“application/json; charset=UTF-8”);
headers.setContentType(type);
headers.add(“Accept”, MediaType.APPLICATION_JSON.toString());
headers.add(“userName”, ocmUserName);
headers.add(“secretKey”,ocmSecretKey);
//JSONObject jsonObj = JSONObject.fromObject(params);

        HttpEntity formEntity = new HttpEntity(headers);

        String result = restTemplate.postForObject(couponUrl, formEntity, String.class);

        LOGGER.info("执行优惠券批次发放定时任务返回"+result.toString());
        EEyeLogSender.info("执行优惠券批次发放定时任务返回"+result.toString());
    }catch (Exception e){
        e.printStackTrace();
        LOGGER.error("定时任务发放优惠券出错.",e);
        EEyeLogSender.error("定时任务发放优惠券出错"+e);
    }

}

@XxlJob(“couponCancellationTask”)
public void couponCancellationTask(){
LOGGER.info(“定时任务开始执行,处理异常优惠卷…”);
EEyeLogSender.info(“定时任务开始执行,处理异常优惠卷…”);
LOGGER.info(“url”+couponCancellationUrl+" username:"+ocmUserName+“ocmSeretKey”+ocmSecretKey);
// 调用定时任务
try{
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType(“application/json; charset=UTF-8”);
headers.setContentType(type);
headers.add(“Accept”, MediaType.APPLICATION_JSON.toString());
headers.add(“userName”, ocmUserName);
headers.add(“secretKey”,ocmSecretKey);
//JSONObject jsonObj = JSONObject.fromObject(params);

        HttpEntity formEntity = new HttpEntity(headers);

        String result = restTemplate.postForObject(couponCancellationUrl, formEntity, String.class);

        LOGGER.info("执行处理异常优惠券定时任务返回"+result.toString());
        EEyeLogSender.info("执行处理异常优惠券定时任务返回"+result.toString());
    }catch (Exception e){
        e.printStackTrace();
        LOGGER.error("执行处理异常优惠券定时任务异常.",e);
        EEyeLogSender.error("执行处理异常优惠券定时任务异常"+e);
    }

}

}

你可能感兴趣的:(java,json,servlet)