-1

I'm trying to learn to create a REST server using jersey & servlet. I referred to this example . This was running earlier but not anymore. Earlier I used to export war file and then starup up the tomcat using commandline & it worked. Latter I added the server in eclipse and started running directly, the url worked and then suddenly stopped working.

I get 404 error almost every time and but yesterday the url worked again after I restart the server and changde some settings.Not sure what I Did. Today I opened eclipse and I'm getting 404 gain.

I think there might be some issue with tomcat setup. I'm new to servlet and tomcat.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns="http://java.sun.com/xml/ns/javaee" 
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
   id="WebApp_ID" version="3.0">
   <display-name>com.aarfid.restserver</display-name>
   <servlet>
      <servlet-name>Jersey RESTful Application</servlet-name>
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
         <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.aarfid.restserver</param-value>
         </init-param>
      </servlet>
   <servlet-mapping>
   <servlet-name>Jersey RESTful Application</servlet-name>
      <url-pattern>/rest/*</url-pattern>
   </servlet-mapping>  
</web-app>

Project Structure

enter image description here

Server Settings

enter image description here

I have referred to

Tomcat 7 keeps giving me a 404. What am I doing wrong?

HTTP Status 404 - The requested resource (/) is not available

EDIT

Also http://localhost:8080 is also giving 404 error, while eclipse shows server is started.

EDIT 2 Tomcat log

Feb 26, 2016 2:17:33 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:WebProject' did not find a matching property.
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/7.0.68
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Feb 8 2016 20:25:54 UTC
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.68.0
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 8.1
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.3
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre7
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.7.0_79-b15
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\RachitaData\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\RachitaData\apache-tomcat-7.0.68-windows-x64\apache-tomcat-7.0.68
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\RachitaData\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\RachitaData\apache-tomcat-7.0.68-windows-x64\apache-tomcat-7.0.68
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\RachitaData\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\RachitaData\apache-tomcat-7.0.68-windows-x64\apache-tomcat-7.0.68\endorsed
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Feb 26, 2016 2:17:33 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\Java\jdk1.7.0_79\bin;C:\Program Files\Git\cmd;.
Feb 26, 2016 2:17:33 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Feb 26, 2016 2:17:33 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Feb 26, 2016 2:17:33 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 756 ms
Feb 26, 2016 2:17:33 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Feb 26, 2016 2:17:33 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.68
Feb 26, 2016 2:17:33 AM org.apache.catalina.loader.WebappClassLoaderBase validateJarFile
INFO: validateJarFile(C:\RachitaData\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\WebProject\WEB-INF\lib\javax.servlet-api-3.0.1.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class
Feb 26, 2016 2:17:34 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Feb 26, 2016 2:17:34 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Feb 26, 2016 2:17:34 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Feb 26, 2016 2:17:34 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1353 ms
Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66

4 Answers4

0

try this :

<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>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.aarfid.restserver</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>resteasy-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

if it's not work add to your question which libs are you use and service class...

structure like that :

src
----main
-------java
--------------packages
-------resourses
-------webapp
----------WEB-INF
--------------web.xml
3vge
  • 197
  • 1
  • 2
  • 15
0

When receiving 404 for even http://localhost:8080
check your Tomcat. - Port 8080 selected -- go to install path or your Tomcat -- folder 'conf'/ File server.xml

there (around row 70) you should find some like

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />

check, what is written there

  • in Eclipse: how do you start your project? -- on your project: 'right button context menu' --> RUN AS 'Run on Server'
0

You actually have to understand what is going on server and how it works. Lets make clear some points of your message

I'm trying to learn to create a REST server using jersey & servlet.

Not really, u already have the server (tomcat in your cause) which u want to serve some rest service.

Rest service is general notion of specially formed http request.

When server receive request it tries to find handler for it. All handlers are defined in web.xml or web project descriptor file. You have just one handler in web.xml

Important lines are

<servlet-name>Jersey RESTful Application</servlet-name>

<url-pattern>/rest/*</url-pattern>

servlet named Jersey RESTful Application suppose to handle request which url correspond to url pattern

Examples:

good url 
http://localhost:8080/rest/author/34

bad url
http://localhost:8080       not found
http://localhost:8080/author/34          not found
http://localhhost:8080/who-is-that-man      not found

Another point worth to mention is that Eclipse Dynamic WebProject will assign WebContext same as project name by default, unless u clearly assign another path (during creation of project or u can change it in project settings).

This will change good url to

good url
http://localhost:8080/WebProjectName/rest/author/34

Another issues can be configuration of server are cached, since eclipse have separate folder for Tomcat Catalina server. In this case it helps to clean tomcat server(i think right click on server, server should be stoped, choose Clean) or just delete and create server again.

simar
  • 1,782
  • 3
  • 16
  • 33
  • I deleted the server and tried by creating war file and starting tomcat manually. I'm now able to run it. But when create a server again in eclipse - I get 404 again – Rachita Nanda Feb 26 '16 at 09:43
  • Check are u using same url to access rest service? – simar Feb 26 '16 at 09:55
  • Try as replace localhost to 127.0.0.1 or cpu IP address, by default Tomcat binds to all available interfaces on cpu. Best thing to do is to set explicitly interface tomcat has to bind in server.xml config file – simar Feb 26 '16 at 10:07
0

When I launch a servlet from Eclipse to run on Tomcat on localhost, the URL has the project name in it. Suppose my project is named Foo, the URL of index.html would be http://localhost:8080/Foo/index.html. http://localhost:8080/index.html returns 404. Have you tried putting the project name in the URL?

Big Pumpkin
  • 3,907
  • 1
  • 27
  • 18
  • BTW, if the project's war file is deployed to a Tomcat container on say Elastic Beanstalk, the project name is then *not* part of the URL. – Big Pumpkin Jun 28 '16 at 07:11