Sekhar Madhiyazhagan
Sekhar Madhiyazhagan

Reputation: 889

simple java web service java

I am new to java web service. while writing sample web service using jax-rs am getting following error in stack trace. run a server which load a page as http status 404. I need to write simple web service for my understanding and then i have to access web service via restful client in android application. am here post my entire web service please help me.

pls lead me in right way. thanks in advance

Aug 26, 2014 6:55:04 AM org.apache.catalina.core.AprLifecycleListener init
 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:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Microsoft\Web Platform Installer\;.
                Aug 26, 2014 6:55:04 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
                WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:webService' did not find a matching property.
                Aug 26, 2014 6:55:04 AM org.apache.coyote.AbstractProtocol init
                INFO: Initializing ProtocolHandler ["http-bio-8080"]
                Aug 26, 2014 6:55:04 AM org.apache.coyote.AbstractProtocol init
                INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
                Aug 26, 2014 6:55:04 AM org.apache.catalina.startup.Catalina load
                INFO: Initialization processed in 699 ms
                Aug 26, 2014 6:55:04 AM org.apache.catalina.core.StandardService startInternal
                INFO: Starting service Catalina
                Aug 26, 2014 6:55:04 AM org.apache.catalina.core.StandardEngine startInternal
                INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
                Aug 26, 2014 6:55:06 AM com.sun.jersey.api.core.PackagesResourceConfig init
                INFO: Scanning for root resource and provider classes in the packages:
                  webService
                Aug 26, 2014 6:55:06 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
                INFO: Root resource classes found:
                  class webService.myserviceClass
                Aug 26, 2014 6:55:06 AM com.sun.jersey.api.core.ScanningResourceConfig init
                INFO: No provider classes found.
                Aug 26, 2014 6:55:06 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
                INFO: Initiating Jersey application, version 'Jersey: 1.17.1 02/28/2013 03:28 PM'
                Aug 26, 2014 6:55:07 AM org.apache.coyote.AbstractProtocol start
                INFO: Starting ProtocolHandler ["http-bio-8080"]
                Aug 26, 2014 6:55:07 AM org.apache.coyote.AbstractProtocol start
                INFO: Starting ProtocolHandler ["ajp-bio-8009"]
                Aug 26, 2014 6:55:07 AM org.apache.catalina.startup.Catalina start
                INFO: Server startup in 2082 ms

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" version="3.0">
      <display-name>webService</display-name>


       <servlet>
        <servlet-name>Rest</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>webService</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Rest</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    </web-app>

myserviceClass.java

package webService;


    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    // Plain old Java Object it does not extend as class or implements 
    // an interface

    // The class registers its methods for the HTTP GET request using the @GET annotation. 
    // Using the @Produces annotation, it defines that it can deliver several MIME types,
    // text, XML and HTML. 

    // The browser requests per default the HTML MIME type.

    //Sets the path to base URL + /hello
    @Path("/hello")
    public class myserviceClass {

      // This method is called if TEXT_PLAIN is request
      @GET
      @Produces(MediaType.TEXT_PLAIN)
      public String sayPlainTextHello() {
        return "Hello Jersey";
      }

      // This method is called if XML is request
      @GET
      @Produces(MediaType.TEXT_XML)
      public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
      }

      // This method is called if HTML is request
      @GET
      @Produces(MediaType.TEXT_HTML)
      public String sayHtmlHello() {
        return "<html> " + "<title>" + "Hello Jersey" + "</title>"
            + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
      }

    } 

Upvotes: 0

Views: 319

Answers (2)

Learner
Learner

Reputation: 21393

The URL you have to access is of the format:

http://your_hostname:port-number/application-context-name/rest/path-name-of-your-web-service

<hostname> is localhost as the application is running on your machine

<port-number> is 8080 - default port for tomcat server

<application-context-name> is name of your application deployed in tomcat server, if you have named it as webService then application context name will be webService

rest -- this is the name of the jersey Servlet configured in your web.xml file, as per your xml it is rest

<path-name-of-your-web-service> -- this will be /hello as per your java web-service code.

So try with:

http://localhost:8080/webService/rest/hello

Upvotes: 2

aluz
aluz

Reputation: 81

I didn't see any error in your tomcat log. Bu I think there is no servlet for your web service which handle the request. (control web.xml) Or you are trying to access wrong context.

You can look this https://netbeans.org/kb/docs/websvc/jax-ws.html tutorial.

Upvotes: 0

Related Questions