java请求http服务-参数是@RequestBody String resultJson 类型
当对方的接口参数是@RequestBody类型时如何调用。
1、对方controller接口写发如下:
@PostMapping(value = "/test")
@ResponseBody
public StringresultBack(@RequestBody String resultJson) {
return "helloWorld";
}
2、Apipost的调用方式如下:

3、用java代码调用对方接口方法
(1)引入依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency> <dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
(2)调用方法
/**
* @param httpUrl 请求的url
* @param jsonParam 原始JSON字符串参数
* @return 响应结果
*/
public String doPostJson(String httpUrl, String jsonParam) {
HttpURLConnection connection = null;
InputStream is = null;
OutputStream os = null;
BufferedReader br = null;
String result = null;
try {
URL url = new URL(httpUrl);
// 通过远程url连接对象打开连接
connection = (HttpURLConnection) url.openConnection();
// 设置连接请求方式
connection.setRequestMethod("POST");
// 设置连接主机服务器超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取主机服务器返回数据超时时间:60000毫秒
connection.setReadTimeout(60000); // 允许向服务器输出数据
connection.setDoOutput(true);
// 允许从服务器读取数据
connection.setDoInput(true); // 设置Content-Type为application/json
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); // 如果需要设置其他请求头,如Authorization,可以在这里添加
// connection.setRequestProperty("Authorization", "Bearer your_token_here"); // 检查JSON参数是否为空
if (jsonParam == null || jsonParam.trim().isEmpty()) {
throw new IllegalArgumentException("JSON参数不能为空");
} // 通过连接对象获取一个输出流
os = connection.getOutputStream();
// 写入JSON参数,指定UTF-8编码
os.write(jsonParam.getBytes("UTF-8"));
os.flush(); // 检查响应状态码
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
is = connection.getInputStream();
} else {
// 非200状态码使用错误流
is = connection.getErrorStream();
} // 处理响应流
if (is != null) {
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer sbf = new StringBuffer();
String temp;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
}
result = responseCode == 200 ? sbf.toString() :
"Error (code: " + responseCode + "): " + sbf.toString();
} else {
result = "No response from server, code: " + responseCode;
}
} catch (MalformedURLException e) {
e.printStackTrace();
result = "URL格式错误: " + e.getMessage();
} catch (IllegalArgumentException e) {
e.printStackTrace();
result = "参数错误: " + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
result = "IO异常: " + e.getMessage();
} finally {
// 关闭资源
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 断开连接
if (connection != null) {
connection.disconnect();
}
}
return result;
}
java请求http服务-参数是@RequestBody String resultJson 类型的更多相关文章
- http 请求参数之Query String Parameters、Form Data、Request Payload
Query String Parameters 当发起一次GET请求时,参数会以url string的形式进行传递.即?后的字符串则为其请求参数,并以&作为分隔符. 如下http请求报文头: ...
- jmeter 的java请求代码在main方法里面执行
1.新建一个java请求执行加法类 public class TestDemo { public int Tdemo(int a,int b){ int sum = 0; sum = a+b; ret ...
- Feign服务调用请求方式及参数总结
前言 最近做微服务架构的项目,在用feign来进行服务间的调用.在互调的过程中,难免出现问题,根据错误总结了一下,主要是请求方式的错误和接参数的错误造成的.在此进行一下总结记录.以下通过分为三种情况说 ...
- 实现在GET请求下调用WCF服务时传递对象(复合类型)参数
WCF实现RESETFUL架构很容易,说白了,就是使WCF能够响应HTTP请求并返回所需的资源,如果有人不知道如何实现WCF支持HTTP请求的,可参见我之前的文章<实现jquery.ajax及原 ...
- SpringMVC中post请求参数注解@requestBody使用问题
一.httpClient发送Post 原文https://www.cnblogs.com/Vdiao/p/5339487.html public static String httpPostWithJ ...
- Netty实现java多线程Post请求解析(Map参数类型)—SKY
netty解析Post的键值对 解析时必须加上一个方法,ch.pipeline().addLast(new HttpObjectAggregator(2048)); 放在自己的Handel前面. ht ...
- java程序利用ansible修改redis服务参数
第一步,java调用shell paraname和paravalue是传给playbook的变量. try { String cmd5 = "sudo ansible-playbook /s ...
- Java 请求webServce接口 带参数
public String getWebServiceByParams(String param){ //获取基金缴付记录 // Post请求的url,与get不同的是不需要带参数 URL postU ...
- 【JMeter】JMeter完成一个java请求的压测
先定义一下我说的remoteService:即远程调用服务,没有http的url.不对外提供或者对外提供有限的服务.具体视各公司的代码架构所定,比如有些公司为web工程,scf服务,db.scf即为服 ...
- Jmeter性能测试之如何写Java请求测试用例类
一. 引言: 最近工作中的一个项目要求做性能测试,该项目由提供服务的几个应用组成,选用的框架是阿里巴巴公司开源的服务框架Dubbo.关于Dubbo的介绍,网上也有很多资料,本人只是做了粗略的了解,没有 ...
随机推荐
- 漏洞预警 | WordPress Plugin Radio Player SSRF漏洞
0x00 漏洞编号 CVE-2024-54385 0x01 危险等级 高危 0x02 漏洞概述 WordPress插件Radio Player是一种简单而有效的解决方案,用于将实时流媒体音频添加到您的 ...
- 【BUG】Python3|安装python3-pip依赖缺失,might want to run ‘apt --fix-broken install‘ to correct these. unment
今天装python,版本装错了. 然后删又删不掉,装pip又装不上,报错是这样的: 想装的时候: 7f2a0f717aa3:~/$ sudo apt-get install python3-pip p ...
- VMware NSX Manager SSL证书更新
安装 NSX 后,管理器节点和集群具有自签名证书.证书有效期为825天,到期后需要进行证书重新更新.如图所示,本环境中此次将有三个类型的证书即将到期需要替换:1.NSX 联合身份验证 PI(Local ...
- Visual Studio 2019 + GLFW + GLAD
!!!如果文件正常引入,但依然各种报错找不到C++源文件,可能是版本问题 即现在是x64,调成x32,以及glfw建议下载32版本 参考文章:https://blog.csdn.net/zjz520y ...
- WPF 的Image 控件 设置 Image.Source 的数据源,可能存在跨线程调用的问题。
相信很多WPF 的开发,应该都很多用到 Image 这个控件来显示图片.这个图片的来源可以来自各种各样的方式获取到. 我们的组内白板.批注的扫码的功能也用到这个去生成二维码,生成后,二维码显示不出来, ...
- SpringSecurity配置 2
SpringSecurity配置 2 目前的现状,虽然是有了登录认证的接口,但是登录完成后,当我们访问受保护的接口时,即使将 Token 令牌携带与请求一起发送,依然是无法请求成功.另外,提示信息如下 ...
- 你了解ConcurrentHashMap吗?ConcurrentHashMap九连问
多线程环境下,使用Hashmap进行put操作会造成数据覆盖,应该使用支持多线程的 ConcurrentHashMap. HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进行 ...
- 使用aqt安装Qt历史版本
首先是安装aqt: Windows 端 pip install -U pip pip install aqtinstall Mac端(可以用pip,也可以用brew) brew install aqt ...
- python之PypI打包whl文件
一.简单介绍 python中我们经常会用到第三方的包作为工具,比如爬虫解析工具,网络请求工具等.之所以要把它封装成包,意识为了技术与业务分离,二是为了能多 项目多平台共用.python里面用到的第三方 ...
- flink基础之window
flink会把数据分成不同的窗口,然后进行汇总和统计. flink的窗口分为timeWindow, countWindow, sessionWindow, gapWindow. timeWindow分 ...