SpringBoot-restTemplateサードパーティAPI呼び出し問題の実装

3668 ワード

SpringBoot-RestTemplate          ,      ,      Map data = new HashMap();      ,    MultiValueMap params= new LinkedMultiValueMap<>();  。

package com.cn.wtsd.service;

import com.alibaba.fastjson.JSONObject;
import com.cn.wtsd.config.WanTongConfig;
import com.cn.wtsd.constant.CodeConstant;
import com.cn.wtsd.entity.ResultModel;
import com.cn.wtsd.util.DESUtils;
import com.cn.wtsd.util.SignUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: Transaction
 * @description:     
 * @author: hantao
 * @create: 2018-12-28 18:06
 */
@Service
public class PayService {

    @Autowired
    private RestTemplate restTemplate;

    public ResultModel pay(){
        Map map = new HashMap();
        map.put("OUTTRADENO", System.currentTimeMillis() + "");
        map.put("PAYAMOUNT", "100");
        map.put("PAYTYPE", "01");
        map.put("CHANNEL", "01");
        map.put("SOURCE", "02");
//        map.put("OPENID", "02");
        map.put("ORDERTITLE", "    ");
        map.put("ID", "100013100101");
        map.put("TERMINALID", "YY100013100101");
        String body="";
        try {
            //    
            String encodeByCBC = DESUtils.encodeByCBC(WanTongConfig.ENCRVPTION_KEY, map);
            System.out.println("encodeByCBC:"+encodeByCBC);
//            SIGN  
            Map requestMap = new HashMap();
            requestMap.put("PARTNERID", "100013");
            requestMap.put("ENCRYPTEDDATA", encodeByCBC);
            String presign = SignUtil.creatSignContent(requestMap, "UTF-8", false);
            presign = presign + "&key="+WanTongConfig.SINGN_KEY;
            System.out.println("       :"+presign);
            String sign = SignUtil.md5(presign, "UTF-8");
            System.out.println("sign:"+sign);
//            Map data = new HashMap();
//            data.put("PARTNERID","100013");
//            data.put("ENCRYPTEDDATA",encodeByCBC);
//            data.put("SIGN",sign);
            MultiValueMap params= new LinkedMultiValueMap<>();
            //       
            params.add("PARTNERID", "100013");             //  
            params.add("ENCRYPTEDDATA", encodeByCBC);
            params.add("SIGN", sign);
//            HttpEntity> requestEntity = new HttpEntity<>(params);
            ResponseEntity responseEntity=restTemplate.postForEntity(WanTongConfig.REQUEST_PAY_URL,params,String.class);
            body=responseEntity.getBody();
        } catch (Exception e) {
//            e.printStackTrace();
            return new ResultModel(CodeConstant.FAIL,"         ");
        }
        return new ResultModel(CodeConstant.SUCCESS,CodeConstant.SUCCESS_MSG,body);
    }

}