首页 > 解决方案 > 无法访问泽西教程中的资源

问题描述

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

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

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

完成所有这些操作后,我无法让“myresource”URL 正常工作。' 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!";
    }
}

标签: javatomcatjersey

解决方案


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

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

然后尝试点击您的网址。应该工作得很好。

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


推荐阅读