Getting 404 error with TOMCAT-Jersey Rest - java

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

Related

Maven Java Web Application Mapping with jersey

Somehow it doesn't work properly
If I am right it should be possible to call showLogin() using projectURI/rest/application/login
But somehow it doesn't work out that way. I guess something went wrong in here/
ApplicationController:
package de.tc;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
#Path("/application")
public class ApplicationController {
#GET
#Path("/login")
public Response showLogin() {
String output = "login";
System.out.println("called");
return Response.status(200).entity(output).build();
}
}
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" version="3.1">
<display-name>application</display-name>
<servlet>
<servlet-name>jersey-serlvet</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>de.tc</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
a URL need to look like:
domain/nameOfProject/rest/pathOfServlet
example http://localhost:8080/HelloWorldProject/rest/application/login
please try to remove the "/" in your path annotation.
you wrote:
#Path("/application")
#Path("/login")
try:
#Path("application")
#Path("login")
please dont forget:
configure your project by server->"add and remove".
before trying to call a Servlet, you must start the server(like tomcat).

Java - Accessing Tomcat/Jersey url gives 404 Not Found

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.

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

How to call rest with jax rs

I am new to jax rs web service. I was studying from this link- http://www.vogella.com/tutorials/REST/article.html
Now when I was trying to do my first rest service i faced some errors.
This is my service code
package de.vogella.jersey.first;
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 {
// This method is called if TEXT_PLAIN is request
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
}
My 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd" 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>de.vogella.jersey.first</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey REST Service</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>de.vogella.jersey.first</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Now when I was running this code I faced HTTP Status 404 error. Please any one help me. I give all the jars from http://jersey.java.net/ Please help me.
If you are using jersey 2.x you Web.xml servlet should be as following
<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>de.vogella.jersey.first</param-value>
</init-param>
</servlet>
In jersey 2.x you need to refer "org.glassfish" and your resource URL would be
localhost:your_port/your_app_name/rest/hello

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