【问题标题】:Restlet: Retrieving http body in verifierRestlet:在验证程序中检索 http 正文
【发布时间】:2013-03-06 16:06:51
【问题描述】:

当我想在我的请求验证器中检索帖子的 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();

它工作正常。

是否有人面临同样的问题或有解决方案?

提前致谢!

【问题讨论】:

    标签: java api rest post restlet


    【解决方案1】:

    实际上,representation默认是一个不存储representation内容的InputRepresentation。

    在您的情况下,最简单的方法是将表示形式包装到您的验证程序中的 StringRepresentation 中:

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

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

    希望对您有所帮助。 蒂埃里

    【讨论】:

    • 耶!这个对我有用。但是我需要更改一行: StringRepresentation sEntity = new StringRepresentation(entity.getText());感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    相关资源
    最近更新 更多