Spring RestTemplate GET 请求参数


@Test
public void testUpdateProfitJson_GET_Params() throws BusinessException {
    String apiURL="UpdateProfitJson";
    /**
     * 组装HTTP_GET请求参数
     */
    Map<String,String> uriVariables =new HashMap<String,String>();
    uriVariables.put("eventId", "3373195");
    uriVariables.put("userSystemRID", "85751812-0370-46CA-987D-FA505B517AAB");
    uriVariables.put("stakeTypeId", "1");
    uriVariables.put("argument", "null");
    uriVariables.put("profit", "3.0");
    String url=baseUrl.append(apiURL).append(createHTTPURLParams(uriVariables)).toString();
    String res=restTemplate.getForObject(url, String.class);
    System.out.println(res);
}

/**
 * 
 * @Title: createHTTPURLParams()
 * @DateTime: 2017年9月19日 下午5:48:55
 * @Description: TODO 构造参数,返回?<>&<> ...
 * @param uriVariables
 * @Return String
 */
public String createHTTPURLParams(Map<String,String> uriVariables) {
    StringBuffer params=new StringBuffer("?");
    for(String keyStr:uriVariables.keySet()) {
        params.append(keyStr+"="+uriVariables.get(keyStr)+"&");
    }
    return params.substring(0, params.length()-1);
}

 或者

@Test
public void testUpdateFactorJson() throws BusinessException {
    String apiURL="UpdateFactorJson?eventId= {eventId}&userSystemRID= {userSystemRID}&stakeId= {stakeId}&factorPercent= {factorPercent}";
    Map<String, String> uriVariables = new HashMap<String, String>();
    uriVariables.put("eventId", "3373195");
    uriVariables.put("userSystemRID", "85751812-0370-46CA-987D-FA505B517AAB");
    uriVariables.put("stakeId", "328163485");
    uriVariables.put("factorPercent", "20.0");
    String res=restTemplate.getForObject(baseUrl.append(apiURL).toString(), String.class, uriVariables);
    System.out.println(res);
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM