测试开发系列之——接口发送功能(二、get请求&json请求)

目录

rest常用的风格

  • get:如项目列表/lemon/project/toList
  • post:
  • put:肯定有体的
  • delete:如删除项目/lemon/project/18

http请求暴露控制层接口。
dubbo请求暴露业务层接口。

get请求

修改ApiServiceImpl.java中的run方法代码如下:

	@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;
	}

接口发送功能get请求调用效果图

测试开发系列之——接口发送功能(二、get请求&json请求)
测试开发系列之——接口发送功能(二、get请求&json请求)

RestTemplate发送HTTP、HTTPS请求

添加分类功能

可参阅:@RequestBody的使用
作用json字符串转java,可以通过fastjson去转。

在ApiClassificationController.java中增加如下代码:

	@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","新增分类成功");
	}
上一篇:同文件不同ajax访问相同文件


下一篇:C# -WebAPIOperator.cs