Jersey with tomcat giving 404 error

人走茶凉 提交于 2019-12-11 19:07:45

问题


I am using Jersey with tomacat 6 and eclipse Juno.

my file name is HelloWorldService :

package com.mkyong.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/hello")
public class HelloWorldService {

@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {

    String output = "Jersey say : " + msg;

    return Response.status(200).entity(output).build();

  }

}

Web.xml:

<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">

<display-name>Restful Web Application</display-name>

<servlet>
    <servlet-name>jersey-serlvet</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>com.mkyong.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

when i am running my project using Apache as server- it is going to this url:

http://localhost:8080/Test/WEB-INF/classes/com/mkyong/rest/HelloWorldService.java

and i am getting 404 error.

I also tried with version="3.0" and it didn't work. Please help me out.


回答1:


you should give your url as follows;

http://localhost:8080/<your-war-name>/rest/hello/<your-param>



回答2:


The address for your example would be:

http://localhost:8080/Test/rest/hello/stranger

and this would output:

Jersey say : stranger

P.S. I guess your WAR name is Test.war and because that your deploy path is localhost:8080/Test



来源:https://stackoverflow.com/questions/14644350/jersey-with-tomcat-giving-404-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!