繁体   English   中英

使用 Tomcat 服务器公开资源

[英]Exposing Resources using Tomcat Server

在调试了几个小时后,我正在寻求帮助以使用 Java 中的 Jersey 设置 REST API。 我使用的是 Tomcat 6 服务器,以下是我的 Mavin 项目的文件结构:

在此处输入图像描述

在右侧,可以看到我要公开的资源。

对于 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 https://github.com/jax-rs -->
<web-app version="2.5">
  <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.howtodoinjava.demo</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>

对于 pom.xml 如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.howtodoinjava.demo</groupId>
<artifactId>JerseyArcheTypeDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>JerseyDemo</name>

<build>
    <finalName>JerseyArcheTypeDemo</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
        <!-- artifactId>jersey-container-servlet</artifactId -->
    </dependency>
    <!-- uncomment this to get JSON support
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>
    -->
</dependencies>
<properties>
    <jersey.version>2.20</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

我可以构建 Maven 项目并启动服务器。 但是,如果我想通过右键单击服务器名称并单击“添加或删除...”将资源添加到服务器,我会收到一条消息,我无法选择任何资源。 我在这个问题中遵循了建议: 没有可以从服务器添加或删除的资源,但它没有帮助,我也没有收到任何可能导致我的错误消息。 任何建议我做错了什么?

当我想通过选择“运行方式”然后“在服务器上运行”来在服务器上运行资源时,我收到以下消息:无法在服务器上运行选择。

在此处输入图像描述

您不能选择此项目,因为 Tomcat 6 不支持Servlet 4.0 项目。 Servlet 4.0需要 Tomcat 9.x,它本身需要Java 1.8 请参阅Tomcat 文档

您可以降低构面版本,但简单地使用较新的 Tomcat 版本更有意义。 Tomcat 6的生命周期已经结束

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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