HttpClient发送POST请求,SpringMVC接收的问题。

thisisgpy 2014-09-09 08:49:23
发送代码:

public static void main(String[] args) throws Exception {
HttpClient client = new DefaultHttpClient();
String path = "http://localhost:8080/TestAnnotationConfig/b";
HttpPost post = new HttpPost(path);
Vendor v = new Vendor();
v.setName("传输数据");
v.setDescription("数据传输");
v.setCreateDate(new Date());
v.setId(20);
String content = JSONBinder.binder(Vendor.class).toJSON(v);
StringEntity entity = new StringEntity(content);
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
post.setEntity(entity);
HttpResponse response = client.execute(post);
System.out.println("响应状态码:" + response.getStatusLine().getStatusCode());
InputStream is = response.getEntity().getContent();
String text = StreamUtil.readInputStream(is);
System.out.println("服务器端响应的数据:" + text);
}


服务器端接收的代码:

@RequestMapping(value = "/b", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String b(@RequestBody Vendor v) {
System.out.println("客户端发送的数据:" + v);
return "success";
}


执行代码报错415。请问怎么回事?
Jackson的HttpMessageConverter我已经配置了,从服务器端返回数据测试已通过,现在就是朝服务器端发送json,报错415。

大神求解,叩谢。
...全文
31211 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xujielaixi 2014-12-01
  • 打赏
  • 举报
回复
解决了吗 怎么解决的
2.wa 2014-09-10
  • 打赏
  • 举报
回复
String content = JSONBinder.binder(Vendor.class).toJSON(v); 不要转 string 直接发对象
优美兴 2014-09-10
  • 打赏
  • 举报
回复

	// 设置HTTP POST请求参数必须用NameValuePair对象
Gson g = new Gson();
				List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
                               //Map键是param,  值是实体
				params.add(new BasicNameValuePair("param", g.toJson(entity)));
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
				// 设置HTTP POST请求参数
				httpPost.setEntity(entity);
  • 打赏
  • 举报
回复
代码看起来没什么问题,什么版本的springmvc?
「已注销」 2014-09-10
  • 打赏
  • 举报
回复
引用 1 楼 u011278496 的回复:
String content = JSONBinder.binder(Vendor.class).toJSON(v); 不要转 string 直接发对象
StringEntity只能接受字符串参数啊,对象怎么直接发送呢?
shop >前言:基于ssm分布式开发实现的电商项目(聚合工程) 注:本项目为开源项目,不能用于商业应用,仅供学习。 ### 使用工具: maven(构建项目),svn(版本控制工具),myeclipse(集成开发环境),nginx(反向代理), FastDFS (图片服务器),tomcat(web服务器),zookeeper(集群管理),mysql(数据库) Junit(测试) ### 技术栈: spring,springmvc,mybatis(框架) solr(搜索服务),redis(缓存),easyUI(后台系统页面) ### 数据库设计 tb_user用户表(id,username,password,phone,email,created,updated) tb_item商品表(id,title,sell_point,price,num,barcode,image,cid,status,created,updated) tb_cat商品分类表(id,parent_id,name,status,sort_order,is_parent,created,updated) tb_item_desc商品描述表(item_id,item_desc,created,updated) tb_item_param商品规格参数表(id,item_cat_id,param_data,created,updated) tb_item_param商品规格参数模板表(id,item_id,param_data,created,updated) tb_order订单表(payment,payment_type,post_fee,status,create_time,update_time,payment_time,consign_time,end_time,close_time,shipping_name,shipping_code,user_id,buyer_message,buyer_nick,buyer_rate) tb_order订单商品表(id,item_id,order_id,num,title,price,total_fee,pic_path) tb_order_shipping订单物流表(order_id,receiver_name,receiver_phone,receiver_mobile,receiver_state,receiver_city,receiver_district,receiver_address,receiver_zip,created,updated) tb_content_category商品目录分类表(id,parent_id,name,status,sort_order,is_parent,created,updated) tb_content商品目录表(id,category_id,title,sub_title,title_desc,url,pic,pic2,content,created,updated) ## 分布式系统 ### 商品后台管理系统 ### shop-manager(管理后台) 商品的添加功能: 1.商品类目选择-easyui异步tree控件的使用 2.图片上传(fastdfs+nginx) 3.富文本编辑器使用KindEditor 4.分页使用PageHelper插件,插件是基于mybatis的拦截器接口实现的 商品的展示功能: 1.分页插件的使用PageHelper。 2.easyUIDataGrid的使用 ### 前台系统 ### shop-rest(发布服务) ### shop-search(搜索服务) * 使用solr实现搜索,内容列表使用redis缓存,使用zookeeper管理集群 ### shop-sso (单点登录系统) SSO英文全称Single Sign On,单点登录。SSO是在多个应用系统中, 用户只需要登录一次就可以访问所有相互信任的应用系统。它包括 可以将这次主要的登录映射到其他应用中用于同一个用户的登录的机制。 它是目前比较流行的企业业务整合的解决方案之一。 用户登录: 1、接收用户名和密码 2、校验用户名密码 3、生成token,可以使用UUID 4、把用户信息写入redis,key就是token 5、把token写入cookie。 6、返回登录成功需要把token返回给客户端。 Session共享的问题: 1、tomcat做集群配置session复制。如果集群中节点很多,会形成网络风暴。推荐节点数量不要超过5个。 2、分布式架构。拆

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧