一:get请求
- 加入依赖
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.4.1</version>
</dependency>
- 写代码
2.1配置OkHttpClient
2.2请求参数
2.3请求头配置
public class Test{
private static OkHttpClient httpClient;
static {
httpClient = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
}
@GetMapping("/getApplicationConfig")
public ApplicationConfigVO getApplicationConfig(@RequestParam("type") int type){
ApplicationConfigVO configVO = new ApplicationConfigVO();
if (type==1){
configVO.setApplicationId(pcApplicationId);
}else {
configVO.setBusinessModelId(businessModelId);
}
return configVO;
}
@GetMapping("/test")
public ApplicationConfigVO test(@RequestParam("type") int type, HttpServletRequest request1){
Response response = null;
ApplicationConfigVO configVO;
try {
Request request = new Request.Builder()
.addHeader("content-type", "application/json")
.addHeader("x-auth0-token",request1.getHeader("x-auth0-token"))
.url("http://localhost:18008/application/getApplicationConfig?type="+type)
.build();
response = httpClient.newCall(request).execute();
String respStr = response.body().string();
JSONObject object = JSONObject.parseObject(respStr);
JSONObject jsonObject = object.getJSONObject("data");
configVO = JSONObject.parseObject(JSONObject.toJSONString(jsonObject), ApplicationConfigVO.class);
} catch (Exception e) {
log.error("获取配置失败:{}", e.getMessage(), e);
throw new RestException("获取配置失败:" + e.getMessage());
} finally {
if (null != response) {
response.close();
}
}
return configVO;
}
}
二:post请求
- 加入依赖
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.4.1</version>
</dependency>
- 写代码
2.1配置OkHttpClient
2.2请求参数
2.3请求头配置
public class Test{
private static OkHttpClient httpClient;
static {
httpClient = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
}
@PostMapping("/getApplicationConfig")
public ApplicationConfigVO getApplicationConfig(@RequestBody SubcategoryVO subcategoryVO, HttpServletRequest request1){
String status = subcategoryVO.getStatus();
int type = Integer.parseInt(status);
ApplicationConfigVO configVO = new ApplicationConfigVO();
if (type==1){
configVO.setApplicationId(pcApplicationId);
}else {
configVO.setBusinessModelId(businessModelId);
}
return configVO;
}
@GetMapping("/test")
public ApplicationConfigVO test(@RequestParam("type") int type, HttpServletRequest request1){
Response response = null;
ApplicationConfigVO configVO;
try {
JSONObject paramObject = new JSONObject();
paramObject.put("status", type);
Request request = new Request.Builder()
.addHeader("content-type", "application/json")
.addHeader("x-auth0-token",request1.getHeader("x-auth0-token"))
.url("http://localhost:18008/application/getApplicationConfig)
.post(okhttp3.RequestBody.create(MediaType.parse("application/json; charset=utf-8"), paramObject.toString()))
.build();
response = httpClient.newCall(request).execute();
String respStr = response.body().string();
JSONObject object = JSONObject.parseObject(respStr);
JSONObject jsonObject = object.getJSONObject("data");
configVO = JSONObject.parseObject(JSONObject.toJSONString(jsonObject), ApplicationConfigVO.class);
} catch (Exception e) {
log.error("获取配置失败:{}", e.getMessage(), e);
throw new RestException("获取配置失败:" + e.getMessage());
} finally {
if (null != response) {
response.close();
}
}
return configVO;
}
}