0

I am following this link for creating a simple Java restApi in Eclipse: https://www.youtube.com/watch?v=5jQSat1cKMo.

However, I am getting a 404 response and console reports an error of

java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer.class

It's a dynamic web project and web.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 <display-name>JavaAPI</display-name>
 <servlet>
   <servlet-name>JAVA_API</servlet-name>
   <servlet-class>org.glassfish.jersey.servlet.ServletContainer.class</servlet-class>
   <init-param>
     <param-name>jersey.config.server.provider.packages</param-name>
     <param-value>test</param-value>
   </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
   <servlet-name>JAVA_API</servlet-name>
   <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>
 </web-app>

The Hello.java is :

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

@Path("/hello")
public class Hello {

@GET
@Produces(MediaType.TEXT_XML)
public String sayHello() {
    String resource = "<? xml version='1.0' ?>" +
"<hello> Hi eesh, hello from xml</hello>";
    return resource;
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public String sayHelloJASON() {
    String resource = null;
    return resource;
}

@GET
@Produces(MediaType.TEXT_HTML)
public String sayHelloHTML() {
    String resource = "<h1> hi eesh, from html</h1>";
    return resource;
}
}

The tomcat version is 8.5, jax-rs-2.25..

localhost:8080 doesn't show the apache home page in the chrome browser and on executing the program on the tomcat server...I get the below response..

enter image description here

I have referred this issue in other links:

org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer Jersey Services with Tomcat and Eclipse

I have tried most of the solutions and none of them seem to work. Moreover, I wanted to do this without converting it to a maven project.

Any help is appreciated.

1

1 Answer 1

0

servlet-class should contain a fully qualified class name. The .class extension is just part of the on-disk file name, and not part of the class name. Remove it.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked but the problem is that the html part of the code is executed first and not the xml part whilst in the tutorial its the other way around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.