@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); }