为什么使用HttpClient发送json总是spring boot 返回json400

为什么使用HttpClient发送json总是返回400_百度知道
为什么使用HttpClient发送json总是返回400
我有更好的答案
每当遇到http错误代码为400,代表客户端发起的请求不符合服务器对请求的某些限制,或者请求本身存在一定的错误。这应该是你的请求数据格式不对
采纳率:85%
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。为什么使用HttpClient发送json总是返回400_百度知道
为什么使用HttpClient发送json总是返回400
我有更好的答案
/api/ddd&quot..。 比如 @RequestMapping(&quot。 特别是用 spring mvc时;) public @ResponseBody JsonResult
doDummy(@RequestParam(&tid')Long id) {
.,容易出现该问题发送请求的参数与后台 处理程序 的要求不匹配
采纳率:94%
来自团队:
为您推荐:
其他类似问题
json的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。最近在做一个接口调用的时候用到Apache的httpclient时候,发现引入最新版本4.5,DefaultHttpClient等老版本常用的类已经过时了,不推荐使用了;去官网看了一下在4.3之后就抛弃了。
可以参考:
①&推荐使用 CloseableHttpClient
②&设置过时参数等类也已经在4.3过后不推荐使用
& DefaultHttpClient --&&CloseableHttpClient
HttpClient httpClient=new DefaultHttpClient(); --& CloseableHttpClient httpClient = HttpClients.createDefault();HttpResponse --& CloseableHttpResponse
HttpResponse httpResponse = httpClient.execute(httpPost); --& CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
* Post方式 得到JSONObject
* @param paramsHashMap post参数
* @param url
* @param encoding 编码utf-8
public JSONObject getJSONObjectByPost(Map&String, String& paramsHashMap, String url, String encoding) {
//创建httpClient连接
CloseableHttpClient httpClient = HttpClients.createDefault();
JSONObject result = null;
List&NameValuePair& nameValuePairArrayList = new ArrayList&NameValuePair&();
// 将传过来的参数添加到List&NameValuePair&中
if (paramsHashMap != null && !paramsHashMap.isEmpty()) {
for (Map.Entry&String, String& entry : paramsHashMap.entrySet()) {
nameValuePairArrayList.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
UrlEncodedFormEntity entity = null;
// 利用List&NameValuePair&生成Post请求的实体数据
// UrlEncodedFormEntity 把输入数据编码成合适的内容
entity = new UrlEncodedFormEntity(nameValuePairArrayList, encoding);
HttpPost httpPost = new HttpPost(url);
// 为HttpPost设置实体数据
httpPost.setEntity(entity);
// HttpClient 发送Post请求
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// CloseableHttpResponse
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(httpEntity.getContent(), "UTF-8"), 10 * 1024);
StringBuilder strBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
strBuilder.append(line);
// 用fastjson的JSON将返回json字符串转为json对象
result = JSON.parseObject(strBuilder.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
public JSONObject getJSONObjectByGet(String url){
JSONObject resultJsonObject=null;
//创建httpClient连接
CloseableHttpClient httpClient = HttpClients.createDefault();
StringBuilder urlStringBuilder=new StringBuilder(url);
StringBuilder entityStringBuilder=new StringBuilder();
//利用URL生成一个HttpGet请求
HttpGet httpGet=new HttpGet(urlStringBuilder.toString());
// HttpClient 发送Post请求
CloseableHttpResponse httpResponse = null;
httpResponse=httpClient.execute(httpGet);
} catch (Exception e) {
e.printStackTrace();
//得到httpResponse的状态响应码
if (httpResponse.getStatusLine().getStatusCode()== HttpStatus.SC_OK) {
//得到httpResponse的实体数据
HttpEntity httpEntity=httpResponse.getEntity();
if (httpEntity!=null) {
BufferedReader reader=null;
reader=new BufferedReader(new InputStreamReader(httpEntity.getContent(), "UTF-8"), 8*1024);
String line=null;
while ((line=reader.readLine())!=null) {
entityStringBuilder.append(line);
// 从HttpEntity中得到的json String数据转为json
String json=entityStringBuilder.toString();
resultJsonObject=JSON.parseObject(json);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
} catch (IOException e) {
e.printStackTrace();
return resultJsonO
本文结合自己经历及收集网络知识综合个人总结,只为分享技术,方便解决问题,如有侵犯版权信息等,请联系我!
阅读(...) 评论()为什么使用HttpClient发送json总是返回400_百度知道
为什么使用HttpClient发送json总是返回400
我有更好的答案
是在打开网页时浏览器返回到客户端的一种状态码。显示在客户端的也就是400页面。400页面是当用户在打开网页时,返回给用户界面带有400提示符的页面;不存在的域名”400是一种是HTTP状态码;;2、invalid hostname意思是&quot:1。其含义是你访问的页面域名不存在或者请求错误、bad request意思是“错误的请求&quot。主要有两种形式,400 Bad Request
【0元入学,两周免费试听】
主营:培训【Python+人工智能,Java大数据,HTML5】
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。1、参数的url就是被调用的地址,map是你要传的参数。参数转成json我使用的是gson方式转换的。
&&& 主要使用的jar包有httpclient-4.5.3.jar、httpcore-4.4.6.jar、commons-codec-1.9.jar、gson-2.2.4.jar和commons-logging-1.2.jar。
  如果发送的post请求想传送文件,需添加httpmime-4.5.3.jar包,并设置如下代码:
&&& HttpEntity multipartEntityBuilder = MultipartEntityBuilder.create().addBinaryBody("file", new File("D:\\workspace\\programm\\WebContent\\programm\\1991.zip")).build();
&&& 第一个参数表示请求字段名,第二个参数就是文件。
&&& 还想添加参数则
&&& HttpEntity multipartEntityBuilder = MultipartEntityBuilder.create().addTextBody("name", "张三").addBinaryBody("file", new File("D:\\workspace\\programm\\WebContent\\programm\\1991.zip")).build();
&&& httpPost.setEntity(multipartEntityBuilder);
import java.io.IOE
import java.util.M
import org.apache.http.HttpE
import org.apache.http.client.ClientProtocolE
import org.apache.http.client.methods.CloseableHttpR
import org.apache.http.client.methods.HttpP
import org.apache.http.entity.StringE
import org.apache.http.impl.client.CloseableHttpC
import org.apache.http.impl.client.HttpC
import org.apache.http.util.EntityU
import com.google.gson.G
public class HttpClientUtil {
private final static String CONTENT_TYPE_TEXT_JSON = "text/json";
public static String postRequest(String url, Map&String, Object& param) throws ClientProtocolException, IOException{
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/charset=UTF-8");
Gson gson = new Gson();
String parameter = gson.toJson(param);
StringEntity se = new StringEntity(parameter);
se.setContentType(CONTENT_TYPE_TEXT_JSON);
httpPost.setEntity(se);
CloseableHttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
&2、返回的结果也可以使用gson转换成对象进行下一步操作。
import com.google.gson.G
public class GsonUtil {
public static &T& T jsonToObject(String jsonData, Class&T& type) {
Gson gson = new Gson();
T result = gson.fromJson(jsonData, type);
public static void main(String[] args) {
String json = "{'id':'1','name':'zhang','address':'Hubei'}";
jsonToObject(json, Person.class);
Person person = jsonToObject(json, Person.class);
System.out.println(person);
建立要转成的对象的类。
import java.util.D
public class Person {
private int
private int
private Spublic int getId() {
public void setId(int id) {
public String getName() {
public void setName(String name) {
this.name =
public int getAge() {
public void setAge(int age) {
this.age =
public String getAddress() {
public void setAddress(String address) {
this.address =
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
&3、发送以键值对形式的参数的post请求
package com.avatarmind.
import java.util.ArrayL
import java.util.L
import org.apache.http.HttpE
import org.apache.http.NameValueP
import org.apache.http.client.entity.UrlEncodedFormE
import org.apache.http.client.methods.CloseableHttpR
import org.apache.http.client.methods.HttpP
import org.apache.http.impl.client.CloseableHttpC
import org.apache.http.impl.client.HttpC
import org.apache.http.message.BasicNameValueP
import org.apache.http.util.EntityU
public class HttpClient3 {
public static void main(String[] args) throws Exception {
CloseableHttpClient client = HttpClients.createDefault();
String url = "http://yuntuapi.amap.com/datamanage/table/create";
HttpPost httpPost = new HttpPost(url);
// 参数形式为key=value&key=value
List&NameValuePair& formparams = new ArrayList&NameValuePair&();
formparams.add(new BasicNameValuePair("key", "290e3ddb64"));
formparams.add(new BasicNameValuePair("name", "火狐"));
// 加utf-8进行编码
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httpPost.setEntity(uefEntity);
CloseableHttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
System.out.println(result);
阅读(...) 评论()

我要回帖

更多关于 java返回json格式数据 的文章

 

随机推荐