How to create simple webserver in Java using Eclipse, Tomcat and Jersey i.e steps to follow?
We are creating simple webserver using the below links:
- http://www.ibm.com/developerworks/web/library/wa-aj-tomcat/
- http://www.vogella.de/articles/REST/article.html
but we got an error like this:
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
Have maven running. Then run this command(press enter if it asks sth):
It will create you a simple webapp. Now create the source package as src/main/java/com/test/rest, and create a simple class as following with a name “test” in it:
At that point you should get errors, resolve them by adding this dependency to your pom:
you can run a dummy “mvn clean install” so that maven will download the repository and your errors will disappear.
Now, go to webapp/WEB-INF and configure your web.xml as follows:
here we said which classes to be loaded and also gave a small prefix with “/rest”. so your webservice will start with this prefix.
Now you are ready, build the app, and add the jar file under tomcat/webapps folder. when you run your tomcat you can reach to your webservice via:
(url_to_tomcat_server/jar_name/prefix_at_web_xml/prefix_at_java_rest_class/dummy_text_requested_byclass)
Note: tested and running