3 Replies Latest reply on Dec 25, 2013 7:08 AM by Yernar Adilbekov

    Problem with REST Service Interceptor

    Yernar Adilbekov Newbie

      I have a problem with the Interceptor. When executing proceed, there is an error: ClassCastException: ServerMessage cannot be cast to com.google.gwt.http.client.Response

      What's the problem?

       

      @Remote
      public interface CustomerService {
          @InterceptedCall(SimpleInterceptor.class)
          public String getCustomerName();
      }
      

       

      @Service
      public class CustomerServiceImpl implements CustomerService{
          @Override
          public String getCustomerName() {
              return "Answer from server";
          }
      }
      

       

      public class SimpleInterceptor implements RestClientInterceptor {
          @Override
          public void aroundInvoke(RestCallContext context) {
              context.proceed(new ResponseCallback() {
                  @Override
                  public void callback(Response response) {
                      int statusCode = response.getStatusCode();
                      if (statusCode == 200) {
                          GWT.log("it's ok");
                      }
                      if (statusCode == 404) {
                          GWT.log("Error 404");
                          // to do something
                      }
                  }
              });
          }
      }
      

       

      Client called the service:

      remoteService.call(new RemoteCallback<ServerMessage>() {
                         @Override
                         public void callback(ServerMessage response) {
                             Window.alert(response.getText()+" called in HeaderController");
                         }
                     }).getMessage();
      
        • 1. Re: Problem with REST Service Interceptor
          Christian Sadilek Master

          Hi,

           

          Unfortunately, using two different type of callbacks (at the call site and in the interceptor) wasn't originally supported. You will need to upgrade to the latest 3.0-SNAPSHOTs or 2.4.4-SNAPSHOTs to make this work.

           

          If you're intercepting the response using a different callback you will also need to make sure to set the result in the call context so that the original callback (the callback provided at the call site) can be invoked.

           

          public class SimpleInterceptor implements RestClientInterceptor {  
                  @Override  
                  public void aroundInvoke(final RestCallContext context) {  
                      context.proceed(new ResponseCallback() {  
                          @Override  
                          public void callback(Response response) {  
                              int statusCode = response.getStatusCode();  
                              if (statusCode == 200) {  
                                  GWT.log("it's ok");  
                                  context.setResult(Marshalling.fromJSON(response.getText()));
                              }  
                              if (statusCode == 404) {  
                                  GWT.log("Error 404");  
                                  // to do something  
                              }  
                          }  
                      });  
                  }  
              } 
          
          • 2. Re: Re: Problem with REST Service Interceptor
            Christian Sadilek Master

            There's also an easier solution that doesn't require you to upgrade (using both a RemoteCallback and an ErrorCallback instead of a ResponseCallback in your interceptor):

             

            public class SimpleInterceptor implements RestClientInterceptor {
                    @Override
                    public void aroundInvoke(final RestCallContext context) {
                      context.proceed(
            
                        new RemoteCallback<ServerMessage>() {
                           @Override
                           public void callback(ServerMessage serverMessage) { 
                             GWT.log("it's ok");
                           }
                        },
            
                        new RestErrorCallback() {
                          @Override
                          public boolean error(Request message, Throwable throwable) {
                            try {
                              throw throwable;
                            } 
                            catch(ResponseException re) {
                              if (re.getResponse().getStatusCode() == 404) {
                                // react to 404
                              }
                              // react to other error codes...        
                            }
                            catch (Throwable t) {
                    
                            }
                            return false;
                        }
                      ));
                    }
                }
            
            
            

             

            Cheers,

            Christian

            • 3. Re: Problem with REST Service Interceptor
              Yernar Adilbekov Newbie

              Christian,tell me please.I used for errai: 3.0.0.20131101-M2. I have a problem with the current task. When I call the service, there is an error: JSONException.

              What am I doing wrong?