1、使用如下jar包
org.apache.httpcomponents
4.5.3
该maven对应如下jar
2、通过HttpClient发送POST请求,并构造出请求json字符串,并添加请求头参数
请求json
{
"conditions": [
{
"studentName": "张三",
"studentSn": 1
}
],
"pager": {
"pageIndex": 6,
"pageSize": 20
}
}
3、客户端代码:
package com.myapp.core;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSON;
import com.myapp.dto.Page;
import com.myapp.dto.Parameters;
import com.myapp.dto.StudentInfoDto;
public class HttpClientApplication {
public static Parameters getParams() {
// 构造请求参数
Page pager = new Page();
pager.setPageIndex(6);
pager.setPageSize(20);
StudentInfoDto dto = new StudentInfoDto();
dto.setStudentSn(1);
dto.setStudentName("张三");
Parameters parameters = new Parameters();
List conditions = new ArrayList();
conditions.add(dto);
parameters.setConditions(conditions);
parameters.setPager(pager);
return parameters;
}
public static void main(String[] args) {
Parameters parameters = getParams();
String params = JSON.toJSONString(parameters);
System.out.println(params);
String url = "http://localhost:8080/myapp/jsonRequest?studentId=20100723";
HttpClient httpClient = null;
HttpPost postMethod = null;
HttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
postMethod = new HttpPost(url);//传入URL地址
//设置请求头
postMethod.addHeader("Content-type", "application/json; charset=utf-8");
postMethod.addHeader("X-Authorization", "AAAA");//设置请求头
//传入请求参数
postMethod.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
response = httpClient.execute(postMethod);//获取响应
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("HTTP Status Code:" + statusCode);
if (statusCode != HttpStatus.SC_OK) {
System.out.println("HTTP请求未成功!HTTP Status Code:" + response.getStatusLine());
}
HttpEntity httpEntity = response.getEntity();
String reponseContent = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);//释放资源
System.out.println("响应内容:" + reponseContent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、服务端代码
对应的controller中的如下:
@ResponseBody
@RequestMapping(value="jsonRequest",method=RequestMethod.POST)
public StudentInfo getStudentInfoBysn(
String studentId,
@RequestBody(required=false) Parameters parameters,
@RequestHeader(value="X-Authorization",required=false) String authorization){
System.out.println("获取的studentId为:" +studentId);
System.out.println("获取的请求参数parameters为:" + parameters);
System.out.println("获取的请求头X-Authorization为:" + authorization);
System.out.println("后续处理数据...");
BigDecimal studentSn = parameters.getConditions().get(0).getStudentSn();
StudentInfo studentInfo = studentService.getStudentInfoBySn(studentSn);
System.out.println("获取数据库查询结果:" + studentInfo);
return studentInfo;
}