org.restlet:针对Web服务发布JSON内容返回HTTP错误411(长度要求) [英] org.restlet: Posting JSON content against webservice returns HTTP error 411 (length required)

查看:144
本文介绍了org.restlet:针对Web服务发布JSON内容返回HTTP错误411(长度要求)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简化的代码示例: http://pastebin.com/9ZQxSXi9

我想试验restlet 2.0库和gpodder Web服务,但不知何故我到达了看不见树木的地步. 该示例中的服务需要HTTP身份验证,并将一些JSON内容发布到URL. 没什么复杂的,但是即使以调试视图要求请求对象包含必要内容的方式,RESTful Web服务的响应也使我相信请求的HTTP标头缺少内容.

I wanted to experiment with the restlet 2.0 library and the gpodder webservice but somehow i reached a point where I can't see the wood for the trees. The service in the example requires HTTP authentication and to post some JSON content to a URL. Nothing that complicated but somehow even though the debug view claims the request object to contain the necessary content the RESTful webservice's response leads me to believe the HTTP header of the request was missing the content.

关于原因的任何想法?预先感谢.

Any ideas on what's the reason? Thanks in advance.

推荐答案

问题是我见过的WriterRepresentation的实现(JsonRepresentation,JacksonRepresentation,XStreamRepresentation)都没有设置对象传递时表示的大小.因此,如果创建新的JacksonRepresentation(map),则不会计算大小.

The problem is that that none of the implementation of WriterRepresentation I've seen (JsonRepresentation, JacksonRepresentation, XStreamRepresentation) set the size of the representation when an object is passed. So if you create a new JacksonRepresentation(map) the size is not calculated.

您必须手动计算地图内容的长度,然后调用Representation.setSize(). 或者像我一样使用

You have to compute manually the length of the map content and calling Representation.setSize(). Or, as I did, use a

new JsonRepresentation(一个json字符串...");

new JsonRepresentation(" a json string... ");

此构造函数能够计算大小,当然,这就是字符串的长度,因此设置了适当的content-length标头,一切都可以顺利进行.

This constructor is able to compute the size, of course, that's the string length, so the proper content-length header is set and everything works smooth.

这篇关于org.restlet:针对Web服务发布JSON内容返回HTTP错误411(长度要求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆