4 Replies Latest reply on Mar 13, 2013 4:06 AM by mp911de

    Simple JAX-RS

    klind

      Hi, I am trying to create a simple JAX-RS.

       

       

      <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
        
                <listener>
                          <listener-class>
                                    org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
                          </listener-class>
                </listener>
        
                <!-- Servlets -->
                <servlet>
                          <servlet-name>Resteasy</servlet-name>
                          <servlet-class>
                                    org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
                          </servlet-class>
                </servlet>
        
                <servlet-mapping>
                          <servlet-name>Resteasy</servlet-name>
                          <url-pattern>/profitmock/*</url-pattern>
                </servlet-mapping>
      
                <!-- Default page to serve -->
                <welcome-file-list>
                          <welcome-file>test.html</welcome-file>
                </welcome-file-list>
        </web-app>
      
      

       

       

      @Path("api")
      @Produces(MediaType.APPLICATION_JSON)
      public class ProfitMock  {
          
                @GET
                @Path("list")
                @Produces(MediaType.APPLICATION_JSON)
          public String list() {
              return "Hello World, I still need some work to be useful!";
          }
        }
      
      

       

       

       

      From the jboss console... 
      
      Registering web context: /profitMock-3.30.2-SNAPSHOT
      

       

      I use SoapUI to test, but I get this response :

       

      HTTP Status 404 - Could not find resource for relative : /profitmock/api/list of full path: http://localhost:8080/profitMock-3.30.2-SNAPSHOT/profitmock/api/list

       

      The

       

      http://localhost:8080/profitMock-3.30.2-SNAPSHOT/

      return the test html page.

        • 1. Re: Simple JAX-RS
          jaysensharma

          Hi,

           

             Try altering your Class "ProfitMock.java" as following:

           

          import javax.inject.Inject;
          import javax.ws.rs.GET;
          import javax.ws.rs.Path;
          import javax.ws.rs.Produces;
          import javax.ws.rs.core.MediaType;
          import javax.ws.rs.ApplicationPath;
          import javax.ws.rs.core.Application;
          
          @ApplicationPath("profitmock")      // ======> ** NOTICE **   Also notice this class extends "Application"
          
          @Path("api")
          @Produces(MediaType.APPLICATION_JSON)
          public class ProfitMock  extends Application  {
          
                    @GET
                    @Path("list")
                    @Produces(MediaType.APPLICATION_JSON)
              public String list() {
                  return "Hello World, I still need some work to be useful!";
              }
            }
          

           

          Depoy your application and then access it as folllowing :   http://localhost:8080/profitMock-3.30.2-SNAPSHOT/profitmock/api/list

           

           

          Basically you will need to follow any of the 3 options in the following link:  https://docs.jboss.org/author/display/AS7/JAX-RS+Reference+Guide

          The ApplicationPath annotation you have to use your POJO will not work, or use web.xml to do that.

           

          try the Attached WAR  :  https://github.com/jaysensharma/MiddlewareMagicDemos/blob/master/Resteasy_JBoss_EAP6.1.Alpha/build/ResteasyService.war

           

          Access the application as following:    http://localhost:8080/ResteasyService/profitmock/api/list

          1 of 1 people found this helpful
          • 2. Re: Simple JAX-RS
            klind

            Nope.. that didn't help

             

            As I reed the documentation, I shouldn't use the javax.ws.rs.core.Application when using JBoss 7.

            • 3. Re: Simple JAX-RS
              klind

              Well this worked...

               

               

              <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
                
                        <!-- Default page to serve -->
                        <welcome-file-list>
                                  <welcome-file>test.html</welcome-file>
                        </welcome-file-list>
                </web-app>
              
              

               

               

              @Path("api")
              @Produces(MediaType.APPLICATION_JSON)
              public class ProfitMock {
              
              
                        @GET
                        @Path("list")
                        @Produces(MediaType.APPLICATION_JSON)
                        public String list() {
                                  return "Hello World, I still need some work to be useful!";
                        }
              }
              
              

               

               

              @ApplicationPath("/profitmock")
              public class JaxRsActivator extends Application {
               }
              
              
              • 4. Re: Simple JAX-RS
                mp911de

                Hi,

                RESTeasy does not discover by itself sub-context paths. You've got to set the resteasy.servlet.mapping.prefix parameter. If you don't set the parameter, RESTeasy expects to be mapped at /. 

                Look here: http://docs.jboss.org/resteasy/docs/2.3.5.Final/userguide/html/Installation_Configuration.html

                 

                Best regards, Mark