Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么HttpRequest在第二次读取时返回NULL?_Java_Web Services_Rest_Httprequest_Restlet - Fatal编程技术网

Java 为什么HttpRequest在第二次读取时返回NULL?

Java 为什么HttpRequest在第二次读取时返回NULL?,java,web-services,rest,httprequest,restlet,Java,Web Services,Rest,Httprequest,Restlet,我使用restletapi在Java中创建web服务。我使用的是2.0稳定版 在这种情况下,我在第二次读取请求时出现NullPointerException错误 为了安全起见,我在Web服务上应用了过滤器。在filter类中,我检查请求是否包含预期参数。如果成功,则调用webservice 在webservice中处理时,我在请求中得到NULL。以下是我的过滤类- public class MyFilter extends Filter { public MyFilter(Contex

我使用restletapi在Java中创建web服务。我使用的是2.0稳定版

在这种情况下,我在第二次读取请求时出现NullPointerException错误

为了安全起见,我在Web服务上应用了过滤器。在filter类中,我检查请求是否包含预期参数。如果成功,则调用webservice

在webservice中处理时,我在请求中得到NULL。以下是我的过滤类-

public class MyFilter extends Filter {

    public MyFilter(Context context) {
        super(context);
    }

    @Override
    protected int beforeHandle(Request request, Response response) {

        int result = STOP;
        try
        {
                String requestSt = request.getEntityAsText();
                // Check for validation 
        } catch (Exception e) {
            e.printStackTrace();
        }


        return result;
    }
}
当我第二次读取请求时,它返回NULL。即使我只是写-

System.out.println(request.getEntityAsText());
在—

String requestSt = request.getEntityAsText();
然后它还为行stringrequestst=..提供NullPointerException


因此,请为我提供多次读取请求的解决方案。

您可以包装传入的请求实体,以确保其内容在第一次读取后不会丢失。为此,您可以利用org.restlet.engine.io.BufferingRepresentation类

这一行应该做到:

request.setEntity(new BufferingRepresentation(request.getEntity());

您可能需要更新到版本2.1,这是当前的稳定版本。

很高兴得到Restlet之父、Restlet大师的回答。我将切换到版本2.1。您是否有适用于Restlet 2.0的解决方案?我们使用的是使用Restlet 2.0的Activiti。似乎可以在2.0中使用StringRepresentation。