找回密码
 立即注册
首页 业界区 业界 java请求http服务-参数是@RequestBody String resultJs ...

java请求http服务-参数是@RequestBody String resultJson 类型

任俊慧 4 天前
当对方的接口参数是@RequestBody类型时如何调用。
 
1、对方controller接口写发如下:
  1. @PostMapping(value = "/test")
  2.     @ResponseBody
  3.     public StringresultBack(@RequestBody String  resultJson) {
  4.         return  "helloWorld";
  5.     }
复制代码
2、Apipost的调用方式如下:
1.png

3、用java代码调用对方接口方法
(1)引入依赖
  1. <dependency>
  2.     <groupId>org.apache.httpcomponents</groupId>
  3.     httpcore</artifactId>
  4.     <version>4.4.10</version>
  5. </dependency>
  6.   
  7. <dependency>
  8.     <groupId>commons-httpclient</groupId>
  9.     commons-httpclient</artifactId>
  10.     <version>3.1</version>
  11. </dependency>
  12.   
  13. <dependency>
  14.     <groupId>org.apache.httpcomponents</groupId>
  15.     httpclient</artifactId>
  16.     <version>4.5.6</version>
  17. </dependency>
复制代码
 (2)调用方法
  1. /**
  2.      * @param httpUrl 请求的url
  3.      * @param jsonParam 原始JSON字符串参数
  4.      * @return 响应结果
  5.      */
  6.     public String doPostJson(String httpUrl, String jsonParam) {
  7.         HttpURLConnection connection = null;
  8.         InputStream is = null;
  9.         OutputStream os = null;
  10.         BufferedReader br = null;
  11.         String result = null;
  12.         try {
  13.             URL url = new URL(httpUrl);
  14.             // 通过远程url连接对象打开连接
  15.             connection = (HttpURLConnection) url.openConnection();
  16.             // 设置连接请求方式
  17.             connection.setRequestMethod("POST");
  18.             // 设置连接主机服务器超时时间:15000毫秒
  19.             connection.setConnectTimeout(15000);
  20.             // 设置读取主机服务器返回数据超时时间:60000毫秒
  21.             connection.setReadTimeout(60000);
  22.             // 允许向服务器输出数据
  23.             connection.setDoOutput(true);
  24.             // 允许从服务器读取数据
  25.             connection.setDoInput(true);
  26.             // 设置Content-Type为application/json
  27.             connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
  28.             // 如果需要设置其他请求头,如Authorization,可以在这里添加
  29.             // connection.setRequestProperty("Authorization", "Bearer your_token_here");
  30.             // 检查JSON参数是否为空
  31.             if (jsonParam == null || jsonParam.trim().isEmpty()) {
  32.                 throw new IllegalArgumentException("JSON参数不能为空");
  33.             }
  34.             // 通过连接对象获取一个输出流
  35.             os = connection.getOutputStream();
  36.             // 写入JSON参数,指定UTF-8编码
  37.             os.write(jsonParam.getBytes("UTF-8"));
  38.             os.flush();
  39.             // 检查响应状态码
  40.             int responseCode = connection.getResponseCode();
  41.             if (responseCode == 200) {
  42.                 is = connection.getInputStream();
  43.             } else {
  44.                 // 非200状态码使用错误流
  45.                 is = connection.getErrorStream();
  46.             }
  47.             // 处理响应流
  48.             if (is != null) {
  49.                 br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  50.                 StringBuffer sbf = new StringBuffer();
  51.                 String temp;
  52.                 while ((temp = br.readLine()) != null) {
  53.                     sbf.append(temp);
  54.                 }
  55.                 result = responseCode == 200 ? sbf.toString() :
  56.                         "Error (code: " + responseCode + "): " + sbf.toString();
  57.             } else {
  58.                 result = "No response from server, code: " + responseCode;
  59.             }
  60.         } catch (MalformedURLException e) {
  61.             e.printStackTrace();
  62.             result = "URL格式错误: " + e.getMessage();
  63.         } catch (IllegalArgumentException e) {
  64.             e.printStackTrace();
  65.             result = "参数错误: " + e.getMessage();
  66.         } catch (IOException e) {
  67.             e.printStackTrace();
  68.             result = "IO异常: " + e.getMessage();
  69.         } finally {
  70.             // 关闭资源
  71.             if (br != null) {
  72.                 try {
  73.                     br.close();
  74.                 } catch (IOException e) {
  75.                     e.printStackTrace();
  76.                 }
  77.             }
  78.             if (os != null) {
  79.                 try {
  80.                     os.close();
  81.                 } catch (IOException e) {
  82.                     e.printStackTrace();
  83.                 }
  84.             }
  85.             if (is != null) {
  86.                 try {
  87.                     is.close();
  88.                 } catch (IOException e) {
  89.                     e.printStackTrace();
  90.                 }
  91.             }
  92.             // 断开连接
  93.             if (connection != null) {
  94.                 connection.disconnect();
  95.             }
  96.         }
  97.         return result;
  98.     }
复制代码
 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册