简体   繁体   中英

404 when accessing Web Service in java using Jersey and Tomcat

I am new to web services. I am using jersey and Tomcat. I have a simple method written as Webservice;

package ex.ws;

@Path("/Example")
public class Example {

  @GET
  @Produces({"application/plain"})
  @Path("/test")
  public String test()   
  {    
    System.out.println("called");  
    return "sdasd";   
  }     
}

my web.xml is

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>Tenant Manager</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>TMWS</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>ex.ws</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>TMWS</servlet-name>
    <url-pattern>/services</url-pattern>
  </servlet-mapping>
</web-app>

and I am accessing the Web Service hosted in my machine as

http://myhost1:8080/tm/services/Example/test

in the ie, but I am getting the 404 exception. My project name is tm.

The server starts without any error. I am getting the welcome message also from a sample jsp page which I have included.

my struts.xml is

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="struts.devMode" value="true" />
  <constant name="struts.ognl.allowStaticMethodAccess" value="true" />
  <constant name="struts.action.excludePattern" value="/services/.*" />
  <include file="struts-default.xml" />
</struts>

Try using @WebServlet instead of @Path . Not sure but that should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM