最近遇到项目需要使用post请求,传的参数中包含map和字符串,在这里记录下
public boolean startPrinter(HashMap<String, Object> map, String templateName, String printerIp, Integer copy) {
try {
String stringMap = JSON.toJSONString(map);
String url = "http://"+jasperIp+":8080/startPrinter";//访问地址
Map<String, Object> params = new HashMap<>();
params.put("map", stringMap);
params.put("templateName", templateName);
params.put("printerIp", printerIp);
params.put("copy", copy);
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(params, headers);
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(10000);
requestFactory.setReadTimeout(10000);
RestTemplate restTemplate = new RestTemplate(requestFactory);
String re = restTemplate.postForObject(url, httpEntity, String.class);
return re.equals("true");
} catch (Exception e){
log.error("请求接口异常:"+ e.getMessage());
return false;
}
}