不同springCloud项目互相调用接口

迈不过友情╰ 2022-05-14 08:16 374阅读 0赞

有A,B两个springCloud项目,B项目的提供restful接口供A项目调用,B项目不在A的注册中心,其实跟普通的http接口调用方法一样

代码

A项目

1.调用接口类

public static String defaultConnection(String method, String path, int timeout, int readTimeout, String data)
throws Exception {
URL url=null;
URLConnection con=null;
HttpURLConnection urlCon=null;
String strResponse=null;
StringBuilder sb=new StringBuilder();
int intResponseCode = HttpURLConnection.HTTP_OK;
OutputStream out=null;
InputStream input=null;

  1. //第1步,建立连接
  2. try \{
  3. url=new URL(path);
  4. //向某个特定协议对象返回表现http资源连接的引用
  5. con=url.openConnection();
  6. \}catch(Exception e) \{
  7. e.printStackTrace();
  8. System.out.println("http连接失败:"+e.getMessage());
  9. \}
  10. //第2步,验证连接的类型,必须是HttpURLConnection
  11. if(!(con instanceof HttpURLConnection))\{
  12. System.out.println("http连接失败,连接类型错误");
  13. \}
  14. //第3步,发送报文
  15. try \{
  16. //表明程序必须把名称/值对输出到服务器程序资源
  17. con.setConnectTimeout(timeout == 0 ? 1000 : timeout);
  18. con.setReadTimeout(readTimeout == 0 ? 1000 : readTimeout);
  19. con.setRequestProperty("Content-Type","application/json");
  20. con.setDoOutput(true);
  21. con.setDoInput(true);
  22. //表明只能返回有用的信息
  23. con.setUseCaches(false);
  24. urlCon=(HttpURLConnection)con;
  25. //设置HTTP请求方法
  26. urlCon.setRequestMethod(method);
  27. //获得输出流对象
  28. out=urlCon.getOutputStream();
  29. DataOutputStream dos=new DataOutputStream(out);
  30. dos.write(data.getBytes("utf-8"));
  31. dos.flush();
  32. \}catch(Exception e) \{
  33. e.printStackTrace();
  34. System.out.println("http发送失败:"+e.getMessage());
  35. \}finally\{
  36. out.close();
  37. \}
  38. //第4步,校验返回状态
  39. try \{
  40. intResponseCode=urlCon.getResponseCode();
  41. \}catch(Exception e) \{
  42. e.printStackTrace();
  43. System.out.println("http返回状态失败:"+e.getMessage());
  44. \}
  45. if(intResponseCode!=HttpURLConnection.HTTP\_OK)\{ //如果不为(HTTP\_OK)200,说明服务器返回错误
  46. System.out.println("request "+ urlCon.getURL() +" fail. response: code="+intResponseCode +
  47. ", message="+urlCon.getResponseMessage());
  48. \}
  49. //第5步,接收报文
  50. try \{
  51. input=urlCon.getInputStream();
  52. //将字节流转换为字符流
  53. BufferedReader br=new BufferedReader(new InputStreamReader(input));
  54. while((strResponse=br.readLine())!=null)\{
  55. sb.append(strResponse);
  56. \}
  57. \}catch(Exception e)\{
  58. e.printStackTrace();
  59. System.out.println("http接收错误:"+e.getMessage());
  60. \}finally\{
  61. input.close();
  62. //关闭和服务器的连接
  63. urlCon.disconnect();
  64. \}
  65. return sb.toString();
  66. \}
  1. 调用方法

public void testtoken(){

  1. String uuid = "\{\\"accessToken\\":\\"123456\\"\}";
  2. System.out.println("uuid=="+uuid);
  3. String responseEntity = "";
  4. try \{
  5. responseEntity = defaultConnection("POST", "http://localhost:50666/receiveToken", 10000, 10000, uuid);
  6. \}catch(Exception e)\{
  7. e.printStackTrace();
  8. \}
  9. System.out.println("responseEntity=="+responseEntity);
  10. \}

B项目

@RequestMapping(value = “/receiveToken”, method = RequestMethod.POST)
public void receiveToken(@RequestBody UserParamDto user,HttpServletResponse response) throws Exception {
System.out.println(user.getAccessToken());
PrintWriter out = null;
try {
out = response.getWriter();
System.out.println(out);
out.write(“{\“loginName\“:\“11223344\“}“);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}

结果

A

70

B

70 1

发表评论

表情:
评论列表 (有 0 条评论,374人围观)

还没有评论,来说两句吧...

相关阅读