0

I have been trying to create a simple Rest API in java by following steps from a YouTube video. I am new to Java and this is my first experience of building a Rest API in Java.

When I try to run the server I initially get a response as:

The requested resource [/JavaAPI/] is not available

and when I try to access the endpoint /rest/hello I get this response:

Type Exception Report

Message Servlet.init() for servlet [Java API] threw exception

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception
javax.servlet.ServletException: Servlet.init() for servlet [Java API] threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:888)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.base/java.lang.Thread.run(Thread.java:832)


Root Cause
java.lang.IllegalArgumentException
    jersey.repackaged.org.objectweb.asm.ClassReader.<init>(ClassReader.java:171)
    jersey.repackaged.org.objectweb.asm.ClassReader.<init>(ClassReader.java:153)
    jersey.repackaged.org.objectweb.asm.ClassReader.<init>(ClassReader.java:425)
    org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener.process(AnnotationAcceptingListener.java:170)
    org.glassfish.jersey.server.ResourceConfig.scanClasses(ResourceConfig.java:915)
    org.glassfish.jersey.server.ResourceConfig._getClasses(ResourceConfig.java:869)
    org.glassfish.jersey.server.ResourceConfig.getClasses(ResourceConfig.java:775)
    org.glassfish.jersey.server.ResourceConfig$RuntimeConfig.<init>(ResourceConfig.java:1206)
    org.glassfish.jersey.server.ResourceConfig$RuntimeConfig.<init>(ResourceConfig.java:1178)
    org.glassfish.jersey.server.ResourceConfig.createRuntimeConfig(ResourceConfig.java:1174)
    org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:345)
    org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:888)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.base/java.lang.Thread.run(Thread.java:832)

I don't know what is exactly causing the issue, I have been stuck at it for around 2 days now.

I am adding my code below

Hello.java:

package test;

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

@Path("/hello")
public class Hello {
    
    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayHello() {
        String resource = "<? xml version ='1.0' ?>" + "<hello> Pinging with XML </hello>";
        return resource;
    }
    
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String sayHelloJSON() {
        String resource = null;
        return resource;
    }
    
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHelloHTML() {
        String resource = "<h1> Pinging with HTML </h1>";
        return resource;
    }
}

web.xml:

<?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_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>JavaAPI</display-name>
  
  <servlet>
    <servlet-name>Java API</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</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>

Any help would be massively appreciated.

Saurav Saha
  • 745
  • 1
  • 11
  • 30
  • The version of Jersey you are using doesn't match the version of the JDK you are using, e.g. see https://stackoverflow.com/questions/52658991/migrate-jersey-project-to-use-java-10-results-in-java-lang-illegalargumentexcept although the version of Jersey you need to use will depend on which JDK you are using. – tgdavies Jan 02 '21 at 08:07
  • Ok, so I am currently using JDK 15. What version of Jersey would you suggest me to use? @tgdavies – Saurav Saha Jan 02 '21 at 08:14
  • 2.31 may work: https://eclipse-ee4j.github.io/jersey.github.io/release-notes/2.31.html – tgdavies Jan 02 '21 at 08:16
  • Incidentally, I have no particular knowledge of this issue, but I just googled "jersey.repackaged.org.objectweb.asm.classreader illegalargumentexception" and the first hit was an SO question which explained the problem. You might want to try that next time you have an issue. – tgdavies Jan 02 '21 at 08:18
  • Ok. So I downloaded the zip for Jersey 2.32 but unlike the older version of Jersey that I was using, it doesn't have Jar files in it. My question is, how do I add Jersey 2.32 into my project @tgdavies. I know, it seems like a stupid question, but I am really inexperienced with Java – Saurav Saha Jan 02 '21 at 09:42
  • You should learn to use maven. That makes it simple to update the versions of your dependencies. You could create a project using this archetype: https://mvnrepository.com/artifact/org.glassfish.jersey.archetypes/jersey-quickstart-webapp – tgdavies Jan 02 '21 at 10:31

0 Answers0