Java - Accessing Tomcat/Jersey url gives 404 Not Found - java

I've been struggling with this for the past two hours. I've looked at many different StackOverflow answers (a lot of questions appear like this one) but not one of them worked for me.
I have a simple 'CountryResources' class, that uses the #Path("/countries"), but when I navigate to localhost:8080/restservices/countries it just gives me a 404 error.
CountryResource class, located in a worldmap.services package:
package worldmap.services;
import model.Country;
import model.CountryService;
import model.ServiceProvider;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/countries")
public class CountryResource {
#GET
#Produces("application/json")
public String getCountries() {
System.out.println("Opgehaald");
CountryService cs = ServiceProvider.getWorldService();
JsonArrayBuilder jab = Json.createArrayBuilder();
for(Country c : cs.getAllCountries()) {
JsonObjectBuilder job = Json.createObjectBuilder();
job.add("code", c.getCode());
job.add("name", c.getName());
job.add("continent", c.getContinent());
job.add("region", c.getRegion());
job.add("surface", c.getSurface());
job.add("population", c.getPopulation());
job.add("government", c.getGovernment());
jab.add(job);
}
JsonArray array = jab.build();
return array.toString();
}
}
Web XML:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd"
metadata-complete="false">
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>worldmap.services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/restservices/*</url-pattern>
</servlet-mapping>
</web-app>

The problem has been found: Maven is not copying the dependencies of the program to the target's lib folder. Will have to try and figure out how to fix that.

Related

Class not found even though it is clearly visible in Web App Libraries

Similar question exists but none of the answers helped.
Starting Tomcat Server 9 on my Eclipse Project gives the error
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer.class
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1364)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1187)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:539)
Even though I can clearly see ServletContainer.class under org.glassfish.jersey.servlet under jersey-container-servlet-core.jar under Web App Libraries.
All the required jars were added to /src/main/webapp/WEB-INF/lib, they seem to be properly imported.
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.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> <!-- Tried Removing this, no difference -->
</servlet>
<servlet-mapping>
<servlet-name>JAVA API</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
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>Hi Varun! This is the sayHello call.</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>Hi Varun! This is the sayHelloHTML call.</h1>";
return resource;
}
}
Project Structure:
Using:
Eclipse: 2021-03
Tomcat: 9
Java: 1.8 (set under BuildPath, ProjectFacets)
JAX-RS 2.0 / Jersey 2.25.x
No Maven, Gradle involved, simply web project, with every jar hand-picked and imported.
Was following this tutorial step-by-step.
Remove the .class suffix
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
instead of
<servlet-class>org.glassfish.jersey.servlet.ServletContainer.class</servlet-class>
You specifiy the class name, not the class object.

Java Rest Server

I've tried multiple reinstallations of tomcat and followed this tutorial:
https://www.youtube.com/watch?v=5jQSat1cKMo
However I always get this error:
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Screenshot here:
This is my current Hello.java class:
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_PLAIN)
public String sayHello() {
String resource="This is some test return text";
return resource;
}
}
Current 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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>JavaAPI</display-name>
<servlet>
<servlet-name>JavaAPI</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>JavaAPI</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
I have tried with and without the extra imports of:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
What am I missing?
UPDATE:
I'm using plain using IDE
I am also using Jersey JAX-RS 2.1
This line in web.xml doesn't look right:
<servlet-class>org.glassfish.jersey.servlet.ServletContainer.class</servlet-class>
The Java Servlet Specification Version 4.0 (page 14-164) states:
The servlet-class contains the fully qualified class name of the
servlet.
But what you have specified is a hybrid of the servlet's fully qualified class name and the servlet's file name, causing Tomcat to look for a servlet that doesn't exist, so you get a 404 error.
Just remove .class from the <servlet-class> value:
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

REST Jersey HTTP Status 404 - The requested resource is not available

I tries looking on stackoverflow to solve this issue, but I can't find anything. Code seems to be OK, but I get a HTTP 404. Could you help me, please?
package rest;
import java.util.ArrayList;
import java.util.List;
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.core.Response;
import domain.Employee;
#Path("/employees")
public class EmployeeResource {
#GET
#Produces(MediaType.TEXT_PLAIN)
public Response getAllEmployees(){
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee("Keith", "Brown", "Official", 27000));
employees.add(new Employee("Jenny", "Pitman", "Trainer", 31000));
String output = employees.toString();
return Response.status(200).entity(output).build();
}
}
and the web.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>VPP JavaEE 18 Rest DWP eu</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- configuration for jax rs -->
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
I go the browser and write:
http://localhost:8080/VPP JavaEE 18 Rest DWP eu/webservice/employees
But I get A HTTP 404 The requested resource is not available with no clues.
However, Tomcat REST servlet IS working, because if I write:
http://localhost:8080/VPP JavaEE 18 Rest DWP eu/blablalba
I receive a different message:
HTTP Status 404 - /project-name/blablalba
I must also mention that it works if I write:
http://localhost:8080/VPP JavaEE 18 Rest DWP eu/index.html

Getting 404 error with TOMCAT-Jersey Rest

I am using JERSEY2.15:-
java class:-
package packages.newJersey;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/rest")
public class SimpleWebService {
private static String versions = "4.1";
#GET
#Produces(MediaType.TEXT_HTML)
public String simpleMessage() {
return "<p>This is a simple REST</p>";
}
#Path("/version")
#GET
#Produces(MediaType.TEXT_HTML)
public String version() {
return "<p>Version Number:</p> " + versions;
}
}
web.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>LatestJersey</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.package</param-name>
<param-value>packages.newJersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/hello/*</url-pattern>
</servlet-mapping>
</web-app>
even though i use display name as :- LatestJersey
by default tomcat is opening:-
http://localhost:8080/RESTFULServiceWithLatestJersey/
and when i hit:-
http://localhost:8080/RESTFULServiceWithLatestJersey/hello/rest
I AM GETTING 404 ERROR
Could someone please help me here?
Everything looks good, except for this
jersey.config.server.provider.package
It should be
jersey.config.server.provider.packages
You're missing the s

Jersey Restful exception

I'm facing Jersey 2.7. This is my service:
package edu.srv.rest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Application;
#Path("/algebra")
public class Algebra extends Application {
#GET
#POST
#Path("/sum")
public int sum(#QueryParam("a") int a, #QueryParam("b") int b) {
return a + b;
}
}
this is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>RS1</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>edu.srv.rest.Algebra</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RS1</servlet-name>
<url-pattern>/algebra/*</url-pattern>
</servlet-mapping>
</web-app>
and this is my project structure:
when I try to deploy (tomcat 6, tomcat 7, tomcat 8) this service as war I got this exception:
java.lang.NoClassDefFoundError: Could not initialize class org.glassfish.jersey.model.ContractProvider$Builder
what am I missing?
org.glassfish.jersey.model.ContractProvider$Builder uses javax.inject.Singleton class which is present in javax.inject jar. Include the same to resolve the issue.

Categories