Receive custom exceptions from Restlet Framework in GWT client

这一生的挚爱 提交于 2019-11-29 12:31:27

As already mentioned the LocationNameException is present in the Response data. Therefore we can get it, just like a normal entity;

...

       public void handle(Request request, Response response)
        {
            if (getClientResource().getStatus().isError())
            {
                LocationNameException lLocationNameException = null;
                boolean serializationError = false;

                try
                {
                    if (response.isEntityAvailable())
                    {
                        if (MediaType.APPLICATION_JAVA_OBJECT_GWT.equals(response.getEntity().getMediaType()))
                        {
                            lLocationNameException = new ObjectRepresentation<LocationNameException>(
                                    response.getEntity().getText(),
                                    (SerializationStreamFactory) MyLocationListServerResourceProxyImpl.this, false)
                                    .getObject();
                        }
                        else
                        {
                            throw new IOException("Can't parse the enclosed LocationNameException.");
                        }
                    }
                }
                catch (Throwable e)
                {
                    serializationError = true;
                    callback.onFailure(new ResourceException(e));
                }

                if (!serializationError)
                {
                    callback.onFailure(lLocationNameException);
                }
            }
            else
            {

...

The ClientProxyGenerator needs to know the exception type (in this case LocationNameException). Therefore we specify the exception in the ClientProxy interface;

    @Post
    void postLocation(LocationDto pLocationDto, AsyncCallback<LocationDto> pResult) throws LocationNameException;

And use getExceptionTypes() or getGenericExceptionTypes() in the ClientProxyGenerator;

    Class<?>[] exceptionTypes = method.getExceptionTypes();
    java.lang.reflect.Type[] genericExceptionTypes = method.getGenericExceptionTypes();

Of course not all REST methods use a custom exception. When getExceptionTypes() returns an empty list we just return the good old status code;

callback.onFailure(new ResourceException(getClientResource().getStatus()));

With help of Jerome Louvel and Thierry Boileau i created a new ClientProxyGenerator() that supports custom exceptions towards a GWT client;

Just specify the exception from the interface in the ServerResourceProxy (ClientProxy)

and voilà

It is possible to use this custom ClientProxyGenerator() in your project right away.

Download custom ClientProxyGenerator

And place it in a package on the server (for example package com.ludus.server.util)

In GWT module XML change the ClientProxyGenerator to the new version on the server;

And you're ready to go with your custom exceptions, but it would be nice if this extension would be integrated in the Restlet framework.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!