get请求:
@GetMapping("/geo")
public HttpEntity geoConvert(
String coords,
String from,
String to
) throws URISyntaxException {
//可能集群,所壹返回list
List<ServiceInstance> instances = discoveryClient.getInstances("bai-service");
ServiceInstance instance = instances.get(0);
// String host = instance.getHost();//获取ip
URI uri = instance.getUri();//获取协议+ip+端口号
CloseableHttpClient httpclient = HttpClients.createDefault();
/* Map<String, Object> param = new HashMap<>();
param.put("coords",coords);
param.put("from",from);
param.put("to",to);
param.put("output", "json");
param.put("out_put", "json");*/
System.out.println("传过来的参数"+from);
//创建URIBuilder
//URIBuilder uriBuilder = new URIBuilder("http://"+"/bai/do");
URIBuilder uriBuilder = new URIBuilder(uri+"/bai/geo");
//URIBuilder uriBuilder = new URIBuilder("http://"+"/bai/geo");
//设置参数
//uriBuilder.setParameter("Param", String.valueOf(param));
uriBuilder.setParameter("from", String.valueOf(from));
uriBuilder.setParameter("to", String.valueOf(to));
uriBuilder.setParameter("coords", String.valueOf(coords));
//创建HttpGet对象,设置url访问地址
HttpGet httpGet = new HttpGet(uriBuilder.build());
System.out.println("发起请求的信息:"+httpGet);
String res ="";
try {
CloseableHttpResponse response = httpclient.execute(httpGet);
// 获取响应实体
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
// 打印响应内容长度
System.out.println("Response content length: " + entity.getContentLength());
// 打印响应内容
System.out.println("Response content: " + EntityUtils.toString(entity));
}
System.out.println("------------------------------------");
return entity;
}catch (Exception e){
log.error("失败", e);
logger.error("操作失败,请求ip={},失败原因:{}", SysLogUtil.getRealIP(),e.getMessage(),SysLogUtil.createSysLog());
GeoConvert geoConvert = new GeoConvert();
geoConvert.setMessage(res);
geoConvert.setErrorMsg(e.toString());
return null;
}
}
post请求:
@RestController
@RequestMapping("/net")
public class NetworkFaultMatchSrvController {
private Logger logger = LoggerFactory.getLogger("httplog");
@Autowired
private NetworkFaultMatchSrvService networkFaultMatchSrvService;
//nacos封装了一个工具类DiscoveryClient 获取服务中心所有清单
@Autowired
private DiscoveryClient discoveryClient;
@ApiOperation(value = "xxx接口")
@RequestMapping(value = "/queryCustomerToJzgz", method = RequestMethod.POST)
public BaseResult queryCustomerToJzgz(@RequestBody Parameters parameters) throws URISyntaxException {
long start = System.currentTimeMillis();
BaseResult br = new BaseResult();
//===========================================
//可能集群,所壹返回list
List<ServiceInstance> instances = discoveryClient.getInstances("net-service");
ServiceInstance instance = instances.get(0);
// String host = instance.getHost();//获取ip
URI uri = instance.getUri();//获取协议+ip+端口号
CloseableHttpClient httpclient = HttpClients.createDefault();
System.out.println("传过来的参数"+parameters);
//创建URIBuilder
//URIBuilder uriBuilder = new URIBuilder("http://"+"/baidu/do");
//URIBuilder uriBuilder = new URIBuilder(uri+"/networkFaultMatchSrv/queryCustomerToJzgz");
//URIBuilder uriBuilder = new URIBuilder("http://"+"/baidu/geo");
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 创建Post请求
HttpPost httpPost = new HttpPost(uri+"/networkFaultMatchSrv/queryCustomerToJzgz");
//设置参数
// 我这里利用阿里的fastjson,将Object转换为json字符串;
// (需要导入com.alibaba.fastjson.JSON包)
String jsonString = JSON.toJSONString(parameters);
StringEntity entity = new StringEntity(jsonString, "UTF-8");
// post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
// 响应模型
CloseableHttpResponse response = null;
System.out.println("发起请求的信息:"+httpPost);
try {
// 由客户端执行(发送)Post请求
response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
System.out.println("响应状态为:" + response.getStatusLine());
String s ="";
if (responseEntity != null) {
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
s=EntityUtils.toString(responseEntity);
System.out.println("响应内容为:" + s);
}
br.setCode("000");
// String result = networkFaultMatchSrvService.queryCustomerToJzgz(parameters);
br.setMessage("查询成功");
br.setData(s);
} catch (Exception e) {
logger.error("xxx接口调用失败,请求ip={},失败原因:{}",SysLogUtil.getRealIP(),e.getMessage(),SysLogUtil.createSysLog());
e.printStackTrace();
}finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
logger.info("xxx接口,请求ip={},耗时:{}ms,入参:{}",SysLogUtil.getRealIP(),System.currentTimeMillis()-start,parameters,SysLogUtil.createSysLog());
return br;
}
}版权声明:本文为qq_40390762原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。