404 Error using Jersey and Apache Tomcat for Web Api - java

I am trying to play around with Eclipse/Java and I am making a Dynamic Web Project in Eclipse. I have followed tutorials online and looked at several other Stackoverflow posts, but i'm still having the same problem.
When I run my server and try to hit my endpoint /test, I get a 404 error. Here is my code:
My Java Class:
package TestPackage;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
#Path("/test")
public class Test {
#GET
public String Hello() {
return "hello";
}
}
My Pom.xml:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>RestDemo</groupId>
<artifactId>RestDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.25</version>
</dependency>
</dependencies>
</project>
I read that if you're using Jersey 2.+, you wouldn't have to fiddle around with your web.xml, so it's the standard format:
<?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>RestDemo</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>
</web-app>
So when I try to call http://localhost:8080/RestDemo/test, I get a 404 error. Any idea what's going on?

In Servlet 2.5 environment, you have to explicitly declare the Jersey container Servlet in your Web application's web.xml deployment descriptor file.
As per https://jersey.java.net/documentation/latest/user-guide.html#deployment.servlet
Example 4.10. Hooking up Jersey as a Servlet
<web-app>
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
...
</init-param>
</servlet>
...
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/myApp/*</url-pattern>
</servlet-mapping>
...
</web-app>

Related

Why am I getting a 404 error when accessing my Spring MVC app URL?

I'm following a tutorial at Udemy for my first Spring-MVC web app. I've configured it using XML for the servlet specification exactly as controlled with a Controller class to return a String.
Navigating to the URL http://localhost:8080/spring-mvc/login I get a 404.
My code:
web.xml:
<web-app 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"
version="3.0">
<display-name>Spring To do List</display-name>
<!--
<welcome-file-list>
<welcome-file>login.do</welcome-file>
</welcome-file-list>
-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/todo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/spring-mvc/*</url-pattern>
</servlet-mapping>
</web-app>
todo-servlet.xml :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.wds" />
<mvc:annotation-driven/>
</beans>
'''
LoginController.java
'''
package com.wds.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
public class LoginController {
#RequestMapping(value = "/login")
#ResponseBody
public String sayHello() {
System.out.println("performing hello");
return "hello world";
}
}
pom.xml:
4.0.0
com.in28minutes
in28Minutes-first-webapp
0.0.1-SNAPSHOT
war
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<verbose>true</verbose>
<source>1.8</source>
<target>1.8</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<contextReloadable>true</contextReloadable>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
My understanding is that if the controller method executes the Dispatcher Servlet should return the view as specified in the servlet config. As no is specified in there it should just return the String from the controller method i.e. "hello world".
Instead I get the 404.
What I'm confused about is in the console, I can see the servlet config has been registered and the "say Hello" controller method mapped to "/login". I've put a System out in the controller method but it's not being output.
Any idea why I get the 404?
Many Thanks
I would suggest to use request mapping on top of the controller
#Controller
#RequestMapping("/spring-mvc")
public class TestController {
#RequestMapping("/login")
#ResponseBody
public String test(){
System.out.println("test is running");
return "test is working";
}
}
and in web.xml
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Advantages: Good to read the code as well as easy to scale the application

Issue on running a JAVA 13 JAX-RS project in Tomcat 9

I'm working on this simple project and have this problem:
HTTP Status 404 – Not Found
Type Status Report
Message /RestTestThree/myService/Calculator/
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.30
I use this setting: JDK 13, Eclipse 2019-12, Tomcat 9.
I tried different URLs and I hope this one is the correct one:
http://localhost:8080/RestTestThree/myService/Calculator/
I tried both model 3 and 4. Tried different dependencies and versions.
When trying to run this project, at some point the Tomcat even won't start, giving the error
Server Tomcat v9.0 Server at localhost failed to start.
My files are as follows.
pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.RestTestThree</groupId>
<artifactId>RestTestThree</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>13</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
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>RestTestThree</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 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>com.Calculator</param-value> <!--comma separated packages -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Simple REST Service</servlet-name>
<url-pattern>/myService/*</url-pattern>
</servlet-mapping>
Calculator.java
package com;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
#Path("/Calculator")
public class Calculator
{
#GET
#Path("/")
#Produces(MediaType.TEXT_PLAIN)
public String hello()
{
return "Working...";
}
#GET
#Path("/Add")
#Produces(MediaType.TEXT_PLAIN)
public String add()
{
return "<div>Sum = 10</div>";
}
}
I had the same problem. After downgrade java 13 to 11 all work perfect. Need convert project to 11 some.

Java 1.8, tomcat 8.5, Jersey 2 - Can't make it work

I went over everything, even made some changes, fixed some problems but still the server sends back 404 even though tomcat itself is up and running. Following is what I did:
pom.xml:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.resty</groupId>
<artifactId>resty</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<!-- JAX-RS -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.25.1</version>
</dependency>
</dependencies>
<build>
<finalName>resty</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
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_2_5.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">
<display-name>resty</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-serlvet</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.resty</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/resty/*</url-pattern>
</servlet-mapping>
</web-app>
Resty.java
package com.resty;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/resty")
public class Resty {
#GET
#Produces(MediaType.APPLICATION_JSON)
#Path("/example")
public String getExample() {
return "{\"result\":\"blah\"}";
}
}
I'm sure I'm missing something but I just can't figure out what it is. When run as - running on server, eclipse opens this url: http://localhost:9090/Resty/ but I see 404. I tried adding more resty/ to the url and also /example (see code above) but all gives 404.
What am I missing?
you are using <url-pattern>/resty/*</url-pattern> in web.xml and using #Path("/resty") in class for class mapping, and use url http://localhost:9090/Resty/ that is wrong. you need to use <url-pattern>/*</url-pattern>. and url will be http://localhost:9090/resty/
you can also use anything into <url-pattern>/*</url-pattern> which you want but you need to use this pattern before controller path in url.

jersey moxy json NoSuchFieldError and IllegalStateException

I tried to use jersey moxy to test json, my maven dependency only has 'jersey-media-moxy' with version 2.22.2.
The web.xml looks like next :
<?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>webapp</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>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<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>
The Json service class is :
#Path("/json")
public class JsonService {
#GET
#Path("/get")
#Produces(MediaType.APPLICATION_JSON)
public JsonObject getJson() {
JsonObject obj = new JsonObject();
obj.setId(1);
obj.setName("json_test");
return obj;
}
#POST
#Path("/post")
#Consumes(MediaType.APPLICATION_JSON)
public Response createJson(JsonObject obj) {
String result = "json object saved : "+obj;
return Response.status(201).entity(result).build();
}
}
However, when I run this project with Tomcat 8.0 in eclipse, and type
http://localhost:8080/webapp/rest/json/get
following error thrown :
A MultiException has 2 exceptions. They are:
1. java.lang.NoSuchFieldError: WADL
2. java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.server.wadl.processor.WadlModelProcessor
org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:394)
.........
root cause
java.lang.NoSuchFieldError: WADL
org.glassfish.jersey.server.wadl.processor.WadlModelProcessor.<init>(WadlModelProcessor.java:96)
.........
My pom 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>webapp</groupId>
<artifactId>webapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<finalName>webapp</finalName>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.22.2</version>
</dependency>
</dependencies>
</project>
What are the problems?
Thanks in advance!

java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map

I am trying to deploy my first rest application using jersey 2.17.
I am using Maven, GlassFish 3.1.2.2 for deployment.
Application runs in eclipse (tomcat), but gives following error when deploying through glassfish admin console.
Error occurred during deployment: Exception while loading the app :
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException:
java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;.
Please see server.log for more details.
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>JerseyExample</groupId>
<artifactId>JerseyExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.17</version>
</dependency>
</dependencies>
</project>
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>JerseyExample</display-name>
<servlet>
<servlet-name>MyJerseyExample</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>jerseyExample.resource</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyJerseyExample</servlet-name>
<url-pattern>/example/*</url-pattern>
</servlet-mapping>
</web-app>
Ok looked at your issue. The External Glassfish v 3.1.2.2 that you are using is JAVA-EE 6 compatible. And the one through which you are running your app in eclipse is JAVA-EE 7 compatible.
Have a look at both the java docs:
Application class Java Doc for EE6
Application class Java Doc for EE7
You will see that the getProperties method got introduced in JAVA-EE 7.
Simply upgrade to glassfish version-4.x that is compatible with JAVA-EE 7. Assuming nothing else breaks should be good.

Categories

Resources