java 将 Jersey 项目(Rest web 服务)运行到 tomcat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10090715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:34:38  来源:igfitidea点击:

Running Jersey project (Rest web service) to tomcat

javarestjerseytomcat7

提问by Dany

I'm try to follow this guide: tutorialbut when I execute on the web browser it returns an error:

我正在尝试遵循本指南:教程,但是当我在网络浏览器上执行时,它返回一个错误:

HTTP Status 404 - /
type Status report
message /
description The requested resource (/rest.api/rest/hello) is not available.

I have installed tomcat7 in /usr/local on my mac and ran it with startup.sh on the terminal. This is my web.xml and Hello.java

我已经在我的 mac 上的 /usr/local 中安装了 tomcat7,并在终端上使用 startup.sh 运行它。这是我的 web.xml 和 Hello.java

<?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>HelloRest</display-name>

<servlet>
<servlet-name>Jersey REST Service</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>rest.api</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
 <servlet-name>Jersey REST Service</servlet-name>
 <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

package rest.api;

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

// POJO, no interface no extends

// 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 Hello {

// 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> ";
}

}

This is my configuration project:

这是我的配置项目:

enter image description here

在此处输入图片说明

Any suggest?

有什么建议吗?

采纳答案by Yogesh Prajapati

Just use following configuration in web.xml

只需在web.xml 中使用以下配置

    <servlet>
        <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

hope this will help you.

希望这会帮助你。

回答by Pradip Bhatt

Try this one.

试试这个。

<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>Your Service Package Name</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</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>

Visit this one : Jersey RESTful

访问这个:Jersey RESTful

回答by Franklin M Gauer III

In more recent versions of Jersey the servlet-class and init-param change. Try the following:

在最新版本的 Jersey 中,servlet-class 和 init-param 发生了变化。请尝试以下操作:

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>Your Service Package Name</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

This worked for me using Jersey version: 2.5.1.

这对我使用 Jersey 版本有效:2.5.1。

回答by Prashant Yewalekar

<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> rest.api </param-value>

</init-param>

<init-param>

    <param-name> com.sun.jersey.api.json.POJOMappingFeature </param-name>

    <param-value> true </param-value>

</init-param>

<load-on-startup>1</load-on-startup>

<servlet-name> jersey-serlvet </servlet-name>

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