java - ReSTLet:在 validator 中检索 http 正文

标签 java api rest post restlet

当我想在请求 validator 中检索帖子的 http 正文时,它会重置我的实体,并且当我想在资源类中获取 http 正文时,我会收到空​​指针异常。

验证者:

JsonRepresentation jsonrep;
        try {
            Representation entity = request.getEntity();
            jsonrep = new JsonRepresentation(entity);
            //bug: entity resets when getJsonObject is being called.
            JSONObject jsonobj = jsonrep.getJsonObject();
            if(companyId != jsonobj.getInt("id_companies")){
                return Verifier.RESULT_INVALID;
            }
...

应用程序资源:

@Post
public Representation addApp(Representation rep) throws Exception{
//rep is null
    JsonRepresentation jsonrep = new JsonRepresentation(rep);

当我不打电话时:

                JSONObject jsonobj = jsonrep.getJsonObject();

它工作得很好。

有人遇到同样的问题或有解决方案吗?

提前致谢!

最佳答案

事实上,默认情况下,表示是一个不存储表示内容的InputRepresentation。

就您而言,最简单的方法是将表示形式包装到验证程序中的 StringRepresentation 中:

Representation entity = request.getEntity();
StringRepresentation sEntity = new StringRepresentation(entity);
request.setEntity(sEntity);

JsonRepresentation jsonrep = new JsonRepresentation(sEntity);

然后,字符串表示形式将自动提供给您的服务器资源方法...

希望对您有帮助。 蒂埃里

关于java - ReSTLet:在 validator 中检索 http 正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15252285/

相关文章:

c# - 通过事件动态更新 rest api c#

rest - 当资源不存在时,从休息服务返回什么?

java - 安卓谷歌地图报错android.view.InflateException : Binary XML file line #6: error initiating class fragment

java - 导入类 Java 问题 Minecraft 插件

Java File.createNewFile() 比 FileOutputStream 慢得多

ruby-on-rails - Rails API 版本控制,路由问题

java - 如何在 springboot API 中自定义来自@query 的响应

api - 如何在jmeter中将一个http请求的值传递到另一个http请求

c# - POST参数仅在Unity3d中始终为null

java - 将 MD5 转换为长