REST service with Java JAX-RS step by step:
- Include following to two mapping in your web.xml
- Create a class for REST Service. The @PATH annotation must be defined at the class level not at the method level.
- Classpath in the war. –
- Build and Deploy in JETTY or any application server.
- path to get to your REST service
<servlet> <servlet-name>javax.ws.rs.core.Application</servlet-name> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>javax.ws.rs.core.Application</servlet-name> <url-pattern>/yourAppURL/*</url-pattern> </servlet-mapping>
/** * @author shirish * @Jul 11, 2016 * */ @Path("/hello") public class HelloWorldClass { /** * */ public HelloWorldClass() { } @GET @Produces("text/plain") public String getClichedMessage() { return "Hello World"; } }
The easiest way to get war working in a java web container is to put all application classes in “yourWebApp/WEB-INF/classes” such that you do not need to fix your application classpath to include jars.
Note: even if you put your application jars in “yourWebApp/WEB-INF/lib”, the jars may not be in classpath properly; therefore, may not have the classes loaded that are necessary.
http://localhost:8080/yourapp/yourAppURL/hello