I am creating webservices in Java using RESTlet API. I am using 2.0 stable version.

In this I am getting NullPointerException error while reading the request for second time.

I have applied the Filter on webservices for security purpose. In filter class I am checking the request contains the expected parameters. If it is successful then the call is made to webservice.

While processing in the webservice I am getting the NULL in request. Following is my Filter class -

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;
    }
}

When I read the request for second time it returns NULL. Even when I just write -

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

before -

String requestSt = request.getEntityAsText();

then also it gives me NullPointerException for the line String requestSt = ....

So please provide me the solution to read the request for multiple times.

有帮助吗?

解决方案

You can wrap the incoming request entity to ensure its content isn't lost after reading it a first time. For this purpose, you can leverage the org.restlet.engine.io.BufferingRepresentation class.

This line should do it:

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

You might need to update to version 2.1 which is the current stable version.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top