【问题标题】:Cannot reach resource in Jersey tutorial无法访问泽西教程中的资源
【发布时间】:2019-11-25 05:45:39
【问题描述】:

我对泽西岛完全陌生,无法让它发挥作用。

我首先在 Eclipse 中使用 Jersey 2 的 Maven 原型、jersey-quickstart-webapp(或类似的东西)构建了一个项目。

然后我将该项目转换为动态 Web 应用程序,以便我可以在 Eclipse 上的 Tomcat 服务器上运行它。

完成所有这些操作后,我无法让“myresource”网址正常工作。 'http://localhost:8080/Testing/webapi/myresource' 什么都不显示。

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web 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.personal.Testing.Testing</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

资源类:

package com.personal.Testing.Testing;

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

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("myresource")
public class MyResource {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
}

【问题讨论】:

    标签: java tomcat jersey


    【解决方案1】:

    来自教程 (https://howtodoinjava.com/jersey/jersey2-hello-world-example/#webxml)

    “首先,运行 maven 来创建你的 war 文件。
    然后将war文件热部署到你的tomcat服务器..."

    然后尝试点击您的网址。应该可以正常工作。

    您似乎正在为您在 Eclipse 中创建的应用程序进行简单的运行。在这种情况下,应用程序将不会部署到您的 Web 服务器(在您的情况下为 Tomcat)。
    您需要将应用程序部署为服务器中的战争,以使 URL 正常工作。

    【讨论】:

      猜你喜欢
      • 2013-05-13
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多