接口参数使用postman调用如图所示,只能使用raw_json格式调用
使用java代码发送http请求实现此种方式的接口调用
public String doPostJson(String url, String putData, String encoding) throws ParseException {
String body = "";
CloseableHttpResponse response = null;
CloseableHttpClient client = HttpClients.createDefault();
// 向指定资源位置上传内容
HttpPost httpPost = new HttpPost(url);
String content_type = HttpHeaders.CONTENT_TYPE;
String accept = HttpHeaders.ACCEPT;
httpPost.addHeader(content_type, "application/json;charset=utf-8");
httpPost.setHeader(accept,"application/json");
httpPost.setEntity(new StringEntity(putData, Charset.forName("UTF-8")));
try {
response = client.execute(httpPost);
System.err.println("status" + response.getStatusLine().getStatusCode());
// 通过response中的getEntity()方法获取返回值
HttpEntity entity = response.getEntity();
if (entity != null) {
body = EntityUtils.toString(entity, encoding);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpPost.abort();
if (response != null) {
EntityUtils.consumeQuietly(response.getEntity());
}
}
System.err.println("body:" + body);
return body;
}