Java发送http请求发送json对象
直接上代码:
http工具类:
public static String httpPostWithjson(String url, String json) throws IOException {
String result = "";
HttpPost httpPost = new HttpPost(url);
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
BasicResponseHandler handler = new BasicResponseHandler();
StringEntity entity = new StringEntity(json, "utf-8");//解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
result = httpClient.execute(httpPost, handler);
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
测试:
String httpURL = "http://localhost:8080/xxxxxx/orgnameRentArea/save.do";
try {
OrgnameRentArea rentArea = new OrgnameRentArea();
rentArea.setProjectId("1");
rentArea.setRegularrentName("1");
BigDecimal bigDecimal = new BigDecimal(2);
rentArea.setRegularuseArea(bigDecimal);
rentArea.setTempraturEarea(1);
rentArea.setIsflag(1);
rentArea.setStartTime("1");
JSONObject jsonObject = JSONObject.fromObject(rentArea);
String result1 = httpPostWithjson(httpURL, jsonObject.toString());
} catch (IOException e) {
e.printStackTrace();
} 后端接收方式:
@RequestBody 这个注解不能少
@RequestMapping("/save")
@ResponseBody
public Map<String, Object> save(HttpServletRequest request, @RequestBody OrgnameRentArea model ) {
return orgnameRentAreaService.save(request, model);
}
DEBUG视图:

Java发送http请求发送json对象的更多相关文章
- Java发送Post请求,参数JSON,接收JSON
/** * 发送post请求 * @param url 路径 * @param jsonObject 参数(json类型) * @param encoding 编码格式 * @return * @th ...
- java模拟post请求发送json
java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main ...
- java Http post请求发送json字符串
最近差点被业务逻辑搞懵逼,果然要先花时间思考,确定好流程再执行.目前最好用的jar包还是org.apache.http. public class HttpClientHelper { private ...
- java模拟post请求发送json数据
import com.alibaba.fastjson.JSONObject; import org.apache.http.client.methods.CloseableHttpResponse; ...
- JAVA发送POST请求携带JSON格式字符串参数
import org.apache.commons.lang.StringUtils; import org.apache.http.HttpEntity; import org.apache.htt ...
- Android Studio利用异步任务AsyncTask发送post请求获取json数据
syncTask,是Android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口实现UI进度更新),最后反馈执行的结果给UI主 ...
- Java模拟POST请求发送二进制数据
在进行程序之间数据通信时我们有时候就需要自定义二进制格式,然后通过HTTP进行二进制数据交互.交互的示例代码如下: public static void main(String[] args) { S ...
- wemall app商城源码中基于JAVA通过Http请求获取json字符串的代码
wemall-mobile是基于WeMall的Android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.分享其中关于通过Http请求获取json字符串的代码供 ...
- 发送Ajax请求获取JSON格式数据
Aspx前端页面: <script type="text/javascript"> $(function () { $.getJSON("Ajax/TestA ...
随机推荐
- cocos2D-X LUA 常用功能封装和工作经验的一些解决方案
--[[ Packaging_KernelEngine.h 文件说明:所有对象在建立时位置是优先的,传入位置参数必须cc.p(X,Y) CurObj:表示要传入当前的对象 将3.10 lua api ...
- spring boot项目开发中遇到问题,持续更新
1.JPA中EntityManager不能执行建表语句,提示要加事务Error:javax.persistence.TransactionRequiredException: Executing an ...
- Windos DNS Client 缓存
要查看 DNS 缓存,请在命令提示符下键入 ipconfig /displaydns. 要从 DNS 缓存中删除该项,请在命令提示符下键入 ipconfig /flushdns. ipconfig.e ...
- Java-Maven-pom.xml-project-dependencies:dependencies
ylbtech-Java-Maven-pom.xml-project-dependencies:dependencies 1.java 调用ddl <!-- java 调用ddl --> ...
- iOS开发NSMutableArray数组越界处理
#import "NSArray+CrashArray.h" #import <objc/runtime.h> @implementation NSObject (Un ...
- [kuangbin带你飞]专题一 简单搜索 - B - Dungeon Master
#include<iostream> #include<cstdio> #include<string> #include<vector> #inclu ...
- mybatis中一对一关系映射
一对一关系中普通的配置方式 一.多表连接查询语句: <select id="selectStudentWithAddress" parameterType="int ...
- Linux-c对一个十六进制数的某一位取反
enum SWITCH_FLAG { SWITCH_ALL_FLAG = , SWITCH_WEB_FLAG = , …… } unsigned int switch_by_bit_value = 0 ...
- (一)phonegap自学---不会java也会写原生app
移动设备如日中天的今天 微软的表现让我们这些.net程序员无不心寒!!幸好每次乱世出英豪,古有java能夸平台,.net跨语言,今有phonegap跨设备 phonegap是什么 狂点这 http:/ ...
- cache方法用于查询缓存操作,也是连贯操作方法之一。
cache方法用于查询缓存操作,也是连贯操作方法之一. cache可以用于select.find和getField方法,以及其衍生方法,使用cache方法后,在缓存有效期之内不会再次进行数据库查询操作 ...