table of Contents
rest usual style
- get: as a list of items / lemon / project / toList
- post:
- put: there must be body
- delete: delete items such as / lemon / project / 18
exposure control layer interface http request.
dubbo request service layer interface are exposed.
get request
The method of modifying the code ApiServiceImpl.java run as follows:
@Override
public ApiRunResult run(ApiVO apiRunVO){
//远程调用
//http用无参构造,https可以用有参构造
RestTemplate restTemplate = new RestTemplate();
String url = apiRunVO.getHost()+apiRunVO.getUrl();
String method = apiRunVO.getMethod();
List<ApiRequestParam> list = apiRunVO.getRequestParams();
LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
LinkedMultiValueMap<String, String> bodyParams = new LinkedMultiValueMap<String, String>();
String paramStr = "?";
for(ApiRequestParam apiRequestParam : list){
if(apiRequestParam.getType()==3){ //头
headers.add(apiRequestParam.getName(), apiRequestParam.getValue());
}else if(apiRequestParam.getType()==1){
paramStr += apiRequestParam.getName()+"="+apiRequestParam.getValue()+"&";
}else{
//body 2 4,注意此时type==1没有处理
bodyParams.add(apiRequestParam.getName(), apiRequestParam.getValue());
}
}
if(!"?".equals(paramStr)){
paramStr = paramStr.substring(0,paramStr.lastIndexOf("&"));
}
System.out.println("1111"+paramStr);
//System.out.println(headers+"\r\n"+bodyParams);
HttpEntity httpEntity = null;
ResponseEntity response = null;
ApiRunResult apiRunResult = new ApiRunResult();
try{
if("get".equalsIgnoreCase(method)){
//httpEntity = new HttpEntity(headers);
httpEntity = new HttpEntity(bodyParams,headers);
response = restTemplate.exchange(url+paramStr,HttpMethod.GET,httpEntity,String.class);
}
else if("post".equalsIgnoreCase(method)){
httpEntity = new HttpEntity(bodyParams,headers);
response = restTemplate.exchange(url,HttpMethod.POST,httpEntity,String.class);
}
apiRunResult.setStatusCode(response.getStatusCodeValue()+"");
HttpHeaders headsResult = response.getHeaders();
//将java转成json的字符串
//第一种方法:jackson
//apiRunResult.setHeaders(new ObjectMapper().writeValueAsString(headsResult));
//第二种方法:fastjson
apiRunResult.setHeaders(JSON.toJSONString(headsResult));
apiRunResult.setBody(response.getBody().toString());
}catch(HttpStatusCodeException e){
//注意此时有调用异常,有body或没有
apiRunResult.setStatusCode(e.getRawStatusCode()+"");
apiRunResult.setHeaders(JSON.toJSONString(e.getResponseHeaders()));
apiRunResult.setBody(e.getResponseBodyAsString());
}
return apiRunResult;
}
Interface call request sending get renderings
RestTemplate sending HTTP, HTTPS request
- http with no-argument constructor
- https can be configured with parameters, authentication parameters to do a series ssl
refer to: RestTemplate sending HTTP, HTTPS request
Add classification
Refer to: @RequestBody using
role json string rotation java, by fastjson to turn.
ApiClassificationController.java increase in the following code:
@PostMapping("/add2")
public Result add2(@RequestBody String jsonStr){
//String[] strArray = jsonStr.split(":");
System.out.println(jsonStr);
String value = jsonStr.substring(jsonStr.indexOf("[")+1,jsonStr.lastIndexOf("]"));
System.out.println(value);
//将jsonStr转成java对象
ApiClassification apiClassification = JSON.parseObject(value, ApiClassification.class);
System.out.println(apiClassification);
apiClassificationService.save(apiClassification);
return new Result("1","新增分类成功");
}