4 Replies Latest reply on Dec 16, 2013 11:12 PM by cristianmiranda

    Errai REST Service call

    cristianmiranda

      Hi guys,

       

           I'm trying to implement some REST services in my application using Errai 2.4.3.Final following this doc: Errai JAX-RS - Errai - Project Documentation Editor

       

           I can not make it to work. When running the application in debug mode, I put a breakpoint in the service implementation and it never gets invoked.

       

           Here's my code:

       

      package app.client.shared.service;
      
      import java.util.List;
      
      import javax.ws.rs.GET;
      import javax.ws.rs.Path;
      import javax.ws.rs.Produces;
      
      import com.magick.models.shared.Strategy;
      
      @Path("enabledstrategies")
      public interface EnabledStrategies {
      
        @GET
        @Produces("application/json")
        public List<Strategy> listAllEnabledStrategies();
      
      }
      

       

      package app.server.service;
      
      import java.util.List;
      
      import javax.inject.Inject;
      
      import app.client.shared.service.EnabledStrategies;
      import app.server.persistence.StrategyDAO;
      
      import com.magick.models.shared.Strategy;
      
      public class EnabledStrategiesImpl implements EnabledStrategies {
      
        @Inject
        private StrategyDAO strategyDAO;
      
        @Override
        public List<Strategy> listAllEnabledStrategies() {
             return this.strategyDAO.getEnabledStrategies();
        }
      
      }
      

       

      and in the client side:

       

      @Inject
      private Caller<EnabledStrategies> enabledStrategies;
      
      enabledStrategies.call(new RemoteCallback<List<Strategy>>() {
                          @Override
                          public void callback(List<Strategy> strategies) {
                               // Do something with the strategies
                          }
                      }).listAllEnabledStrategies();
      

       

      Am I missing something?

       

      Thanks!

        • 1. Re: Errai REST Service call
          csa

          Hi Cristian,

           

          An easy way to find out what's wrong is to take a look at the request your browser sends when listAllEnabledStrategies() is invoked on the client. You can use FireBug (click on the Net tab) or in Chrome (View->Developer Tools->Network tab).

           

          You can also try to type the URL directly in your browser and check for the response.

           

          A mistake I can think of is not configuring the root path of your REST endpoint properly (see here for more details).

           

          Cheers,

          Christian

          1 of 1 people found this helpful
          • 2. Re: Errai REST Service call
            cristianmiranda

            Cool, so after setting my REST endpoint this way:

             

            RestClient.setApplicationRoot("/MyJaxRsEndpointPath");


            the URL would be: www.myapp.com/MyJaxRsEndpointPath/myServicePath??


            Should I add some additional configuration on my web.xml?


            Thanks!

            • 3. Re: Errai REST Service call
              csa

              Yes. If you've started from the errai-tutorial project you will already have a configuration in there that registers all REST endpoints under /rest:

              https://github.com/errai/errai-tutorial/blob/master/src/main/webapp/WEB-INF/web.xml#L10

               

              There are multiple ways to configure RestEasy depending on what server you intend to use:

              RESTEasy JAX-RS

               

              Cheers,

              Christian

              • 4. Re: Re: Errai REST Service call
                cristianmiranda

                Thanks! added the following to my web.xml and is working now... The only issue I'm having is that all the injected beans in my service are null. Is this because service is not being retrieved from the container? How could I solve this?

                 

                    <!-- Auto scan REST service -->
                    <context-param>
                        <param-name>resteasy.scan</param-name>
                        <param-value>true</param-value>
                    </context-param>
                
                
                    <!-- this need same with resteasy servlet url-pattern -->
                    <context-param>
                        <param-name>resteasy.servlet.mapping.prefix</param-name>
                        <param-value>/rest</param-value>
                    </context-param>
                
                
                    <listener>
                        <listener-class>
                            org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
                        </listener-class>
                    </listener>
                
                
                    <servlet>
                        <servlet-name>resteasy-servlet</servlet-name>
                        <servlet-class>
                            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
                        </servlet-class>
                    </servlet>
                
                
                    <servlet-mapping>
                        <servlet-name>resteasy-servlet</servlet-name>
                        <url-pattern>/rest/*</url-pattern>
                    </servlet-mapping>
                

                 

                Thank you again for replying so fast! you guys rock!