title: REST web application in Maven using Jersey
body: <p>I'm new in jersey and trying to create project that uses rest web service and response me the json. following is my pom.xml</p>
  
  <pre><code>&lt;project
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
  xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;parent&gt;
      &lt;groupId&gt;com.modaltestapp&lt;/groupId&gt;
      &lt;artifactId&gt;modaltestapp&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;/parent&gt;
  &lt;groupId&gt;com.modaltestapp&lt;/groupId&gt;
  &lt;artifactId&gt;modaltestapp-api&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;packaging&gt;war&lt;/packaging&gt;
  &lt;name&gt;modaltestapp-api Maven Webapp&lt;/name&gt;
  &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
  &lt;properties&gt;
      &lt;slf4j.version&gt;1.7.9&lt;/slf4j.version&gt;
      &lt;logback.version&gt;1.1.2&lt;/logback.version&gt;
  &lt;/properties&gt;
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;3.8.1&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
          &lt;version&gt;1.9&lt;/version&gt;
      &lt;/dependency&gt;
  
  
  
  &lt;/dependencies&gt;
  &lt;build&gt;
      &lt;finalName&gt;modaltestapp-api&lt;/finalName&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p></p>
  
  <p>web.xml</p>
  
  <pre><code>&lt;web-app id="WebApp_ID" version="2.4"
  xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
  &lt;display-name&gt;Restful Web Application&lt;/display-name&gt;
  
  
  &lt;servlet&gt;
  &lt;servlet-name&gt;helloworld&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt;
          &lt;param-value&gt;com.modaltestapp.service&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;com.sun.jersey.api.json.POJOMappingFeature&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;helloworld&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p></p>
  
  <p>And java file is:</p>
  
  <pre><code> @GET
  @Path("/print")
  @Produces({ MediaType.APPLICATION_JSON }) // add MediaType.APPLICATION_XML
                                              // if you want XML as well
                                              // (don't forget
                                              // @XmlRootElement)
  public List&lt;RegistrationDto&gt; getAllMessages() throws Exception {
  
      List&lt;RegistrationDto&gt; messages = new ArrayList&lt;RegistrationDto&gt;();
  
      RegistrationDto m = new RegistrationDto();
  
      m.setFirstName("Nabi");
      m.setLastName("Zamani");
      m.setAge(30);
  
      messages.add(m);
  
      System.out.println("getAllMessages(): found " + messages.size() + " message(s) on DB");
  
      return messages; // do not use Response object because this causes
                          // issues when generating XML automatically
  }
  </code></pre>
  
  <p>but when I build and run app it shows me error:</p>
  
  <pre><code> INFO: Starting Servlet Engine: Apache Tomcat/9.0.0.M21
   Jul 03, 2017 11:12:24 AM org.apache.catalina.core.ApplicationContext log
    INFO: Marking servlet [helloworld] as unavailable
   Jul 03, 2017 11:12:24 AM org.apache.catalina.core.StandardContext 
   loadOnStartup
   SEVERE: Servlet [helloworld] in web application [/modaltestapp-api] threw 
    load() exception
   java.lang.ClassNotFoundException: 
   com.sun.jersey.spi.container.servlet.ServletContainer
  at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275)
  at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1109)
  at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:508)
  at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:489)
  at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:119)
  at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1050)
  at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:989)
  at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4921)
  at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5231)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
  at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1439)
  at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1429)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
  at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
  at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:953)
  at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:872)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
  at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1439)
  at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1429)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
  at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
  at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:953)
  at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
  at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
  at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:793)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
  at org.apache.catalina.startup.Catalina.start(Catalina.java:656)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:355)
  at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:495)
  
  Jul 03, 2017 11:12:24 AM org.apache.coyote.AbstractProtocol start
  INFO: Starting ProtocolHandler ["http-nio-8080"]
  Jul 03, 2017 11:12:24 AM org.apache.coyote.AbstractProtocol start
  INFO: Starting ProtocolHandler ["ajp-nio-8009"]
  Jul 03, 2017 11:12:24 AM org.apache.catalina.startup.Catalina start
  INFO: Server startup in 317 ms
  </code></pre>
  
  <p>How can I fix this? I'm using Apache Tomcat 9.</p>