0

After debugging for a few hours, I am seeking help to set up a REST API using Jersey in Java. I am using a Tomcat 6 server and the following is my file structure of the Mavin project:

enter image description here

On the right, the resource which I want to expose can be seen.

For the web.xml I have a file as follows:

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

and for the pom.xml as follows:

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

I can build the Maven project and start the server. However if I want to add resources to the Server by right-clicking on the server name and clicking on "Add or Remove..." I get a message that I cannot select any resources. I followed the adivce in this question: There are No resources that can be added or removed from the server but it did not help and I also do not get any error messages which could lead me. Any recommendation what I am doing wrong?

When I want to run the Resource on the server by selecting "Run As" and then "Run on Server" I get the following message: The selection cannot be run on the server.

enter image description here

nitind
  • 19,089
  • 4
  • 34
  • 43
christheliz
  • 176
  • 2
  • 15
  • 1
    Which Tomcat server? You're not showing the error you're actually running up against. – nitind Jul 05 '22 at 00:39
  • I added the error message now – christheliz Jul 05 '22 at 00:58
  • Open the Properties dialog of the project and list or screenshot the versions of the installed facets on the Project Facets page. Also, which Java runtime did you set for that Tomcat installation in the Server / Runtime Environments preference page? Typically what you're encountering is a mismatch between all of these values, so you need to include all of these values in the question. – nitind Jul 05 '22 at 01:12
  • I am using Version 4.0 for the Dynamic Web Module, Java1.8, JavaScript1.0, and JAX-RS (REST Web Services)1.1 for the project. In itself, the requirements fit for the project. I would have to check for the Tomcat installation – christheliz Jul 05 '22 at 01:19
  • Those are better to put *in the question*, but you can't select this project because Tomcat 6 does not support Servlet 4.0 projects. 4.0 requires Tomcat 9.x, which itself requires Java 1.8. – nitind Jul 05 '22 at 01:20

1 Answers1

2

You can't select this project because Tomcat 6 does not support Servlet 4.0 projects. Servlet 4.0 requires Tomcat 9.x, which itself requires Java 1.8. See the Tomcat documentation.

You can lower the facet version, but it makes far more sense to simply use the newer Tomcat version. Tomcat 6 is far past its end-of-life.

nitind
  • 19,089
  • 4
  • 34
  • 43
  • Thanks for the useful hint! I am using Elipse 4.23 and got the WTP Tools using the Eclipse "Install new software" option but the newest option I get is Tomcat 6. I read that Eclipse for Java EE developers would include it but am not sure if I should install it on the same machine as my current Eclipse IDE. Downloading Tomcat 9 manually and adding it to the Runtime Environment did not work either. – christheliz Jul 05 '22 at 22:25
  • "Install New Software" requires an Update Site URL. Which did you use? It should have been https://download.eclipse.org/releases/2022-03 . – nitind Jul 05 '22 at 23:11
  • I used download.eclipse.org/releases/latest for the downloads. Maybe that is the issue. Thank you so much for your help so far, I am working hard to turn in my thesis within the next week, and being new to Java is kind of a struggle when working on time-sensitive projects. I will check everything with Tomcat 9 and then accept your answer asap – christheliz Jul 06 '22 at 17:44
  • Installing everything from download.eclipse.org/releases/2022-03 instead of download.eclipse.org/releases/latest really did make the difference. There are still errors now but I think that is because of the routing. Anyways, again, thanks for the help. Much appreciated! – christheliz Jul 08 '22 at 04:33