HTTP 404 error while buiding REST API on Java - java

I am starting with restapi and thought of doing it with a basic program found on internet.
Sharing you the code.
The class File:
package com.java2novice.restful;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
#Path("/publish")
public class RestEasyExample {
#GET
#Path("/{message}")
public Response publishMessage(#PathParam("message") String msgStr){
String responseStr = "Received message: "+msgStr;
return Response.status(200).entity(responseStr).build();
}
}
The web.xml-
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- Auto scan REST service -->
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
The 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>RestfulWebServices</groupId>
<artifactId>RestfulWebServices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
I am using the jersey 2.0.
I have deployed the war and placed it in webapps folder of Tomcat.
I have tried calling it using two urls-
http://localhost:8080/RestfulWebServices/publish/abc
and
http://localhost:8080/RestfulWebServices/publish?message=abc
But i still keep on getting HTTP 404 not found.
Please assist me in where i am missing something.
Thanks

I think you are mixing resteasy with jersey.
In either way, the <listener> is required param in web.xml
Try adding following in your web.xml file:
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
You should also consider using restEasy's servlet container rather using Jersey.
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>

Try using rest application container with this configuration over web.xml configuration.
package com.java2novice.config;
import org.glassfish.jersey.server.ResourceConfig;
import java.util.Collection;
#javax.ws.rs.ApplicationPath("v1")
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
// package where all your api lives
packages("com.java2novice.restful");
}
#Override
public Collection<Strinzg> getPropertyNames() {
return super.getPropertyNames();
}
}
Then visit /v1/your-endpoint

Related

basic Jersey webservice with maven jetty plugin

I have been working with the restfull webservices provide by Jersey in combination with the maven jetty plugin at my previous job. But now for a personal project i am starting from scratch by myself and i cant seem to get even the basic version going. I have tried to follow a few tutorial but they are either old or off topic.
The most recent example i tried was:
jersey 2 + spring 4 + jetty-maven-plugin
So the below configs make the plug in run but no matter which url i try it gives me 404's
pom:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<packaging>war</packaging>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<jetty.version>9.3.11.v20160721</jetty.version>
<jersey.version>2.23.1</jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
</plugin>
</plugins>
</build>
</project>
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>test</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.foo.bar</param-value>
</init-param>
<!--<load-on-startup>1</load-on-startup>-->
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
resourceConfiguration.java
package com.foo.bar;
import org.glassfish.jersey.server.ResourceConfig;
public class ResourceConfiguration extends ResourceConfig {
public ResourceConfiguration() {
register(entrypoint.class);
}
}
and my service class
package com.foo.bar;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
public class entrypoint {
#GET
#Path("/test")
#Produces(MediaType.TEXT_PLAIN)
public String test() {
return "it works";
}
#GET
#Path("/")
#Produces(MediaType.TEXT_HTML)
public String test2() {
return "it works";
}
}
Nothing fancy right i feel like this should work out of the box.
The Jetty server starts but like i said i get jetty 404 pages at every url permutation i try.
Thanks in advance for any and all input.
Few Changes need to be done to make it work. please follow the below steps.
Step 1: change your web.xml like below with the POJO mapping and Resourse configuration class instead of package name.
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>bar.com.ravi.ResourceConfiguration</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Step 2: change your ResourceConfiguration class like below with package information.
public class ResourceConfiguration extends ResourceConfig {
public ResourceConfiguration() {
packages("bar.com.ravi");
register(Entrypoint.class);
}
}
Step 3: Change your EntryPoint class with Class level #Path Annotation.
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/hi")
public class Entrypoint {
#GET
#Path("/test")
#Produces(MediaType.TEXT_PLAIN)
public String test() {
return "it works";
}
#GET
#Path("/")
#Produces(MediaType.TEXT_HTML)
public String test2() {
return "it works";
}
}
Step 4: Change your POM.xml in build tag like below.
<build>
<finalName>bar</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.11.v20150529</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/bar</contextPath>
</webApp>
<httpConnector>
<port>8888</port>
</httpConnector>
</configuration>
</plugin>
</plugins>
</build>
Step 5: run below command your command promt.
mvn jetty:run
Step 6: hit the browser with below URL
http://localhost:8888/bar/hi/test
The obvious thing that is wrong on your configuration is
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.foo.bar</param-value>
</init-param>
I believe this must be
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.foo.bar.ResourceConfiguration</param-value>
</init-param>
then add the package where your resource classes are currently sitting.
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.foo.bar</param-value>
</init-param>
Try this:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<webApp>
<contextPath>/bar</contextPath>
</webApp>
</configuration>
</plugin>
and direct your browser to http://localhost:8080/bar/test, for example.

404 error with a simple RESTeasy service on tomcat 8 running with eclipse luna

I always end up with a 404 response.
Resource not found. Testing with tomcat
v8.0. Below are the
details.
URL : http://localhost:8080/ESTServer/rest/message/hello
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>ESTServer</groupId>
<artifactId>ESTServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>JBoss repository</id>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.13.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.0.13.Final</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://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>ESTServer</display-name>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.mota.rest.CaDistributionApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Service code :
package com.mota.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
#Path("/message")
public class CaDistributionService {
#GET
#Path("/{param}")
public Response printMessage(#PathParam("param") String msg) {
String result = "Restful example : " + msg;
return Response.status(200).entity(result).build();
}
}
project structure
CaDistributionApplication.java
package com.mota.rest;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
import com.mota.rest.CaDistributionService;;
public class CaDistributionApplication extends Application{
private Set<Object> singletons = new HashSet<Object>();
public CaDistributionApplication() {
singletons.add(new CaDistributionService());
}
#Override
public Set<Object> getSingletons() {
return singletons;
}
}
What am I missing here ?
Please help !!!!
I just tried restarting the eclipse.
Multiple clean/build/refresh/install.
Checking the Target folder for generated
war and classes. Launching tomcat in debug mode.
But basically didn't change anything in the code.
And yes it works finally.

REST api 404 on mapping

I am trying to create a rest api using jersey on glassfish 4 server.
Currently i am getting error 404 whenever I go to my mapping:
http://localhost:8080/LibraryRestTest/api/books
Sadly i get 0 errors on my glassfish console log. I have also tried manually deploying my war on glassfish admin console to result in the same error.
Is there something I haven't yet implemented ?
All files i have created in my project :
glassfish-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
<context-root>/LibraryRestTest</context-root>
</glassfish-web-app>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>Jersey Web Application</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.koen.library
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
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.koen</groupId>
<artifactId>LibraryRestTest</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>WebContent\WEB-INF\web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jersey.version>2.22</jersey.version>
<parser.version>2.22</parser.version>
</properties>
<dependencies>
<dependency>
<groupId>com.koen.library</groupId>
<artifactId>libraryJPA</artifactId>
<version>0.0.1</version>
</dependency>
<!-- Jersey -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${parser.version}</version>
</dependency>
</dependencies>
</project>
BookResource.java
package com.koen.library;
import java.util.ArrayList;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import com.koen.library.pojo.Book;
#Path("books")
public class BookResource {
#GET
#Produces("application/json")
public ArrayList<Book> getAll(){
return new ArrayList<Book>(){{add(new Book(6, "The Brothers Karamazov", "Fyodor Dostoevsky"));}};
}
#GET
#Path("/{id}")
public Book getById(#PathParam ("id") int id){
return null;
}
}
Instead of mapping a jersey servlet in web.xml, you could configure your REST endpoint using Java code using standard Java EE 7 API (Application and #ApplicaionPath:
#ApplicationPath("api")
public class ApplicationConfig extends Application {
}
This will automatically attach all resources annotated with #Path to the api suffix.
If you do not want all resources to map to this suffix, you may list each class in particular, if you override getClasses() method:
#ApplicationPath("api")
public class ApplicationConfig extends Application {
#Override
public Set<Class<?>> getClasses() {
return Arrays.asList(com.koen.library.BookResource.class);
}
}
The fix to my problem was:
<param-value>true</param-value>
I had to add that line in my web.xml.
Now i don't get any 404 error on my mapping.
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>Jersey Web Application</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.koen.library
</param-value>
</init-param>
<param-value>true</param-value>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>

HTTP ERROR 503: can not access /. Reason Service Unavailable

My code is in github
https://github.com/shiblybcc/blog-aggregator
I have just created a spring framework project. This is my web.xml file code
<?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>Course Rating</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>
After I run my jetty server this works perfect and show the content of index.jsp file. But if I add following code in the web.xml file
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.htm</url-pattern>
<url-pattern>*.json</url-pattern>
<url-pattern>*.xml</url-pattern>
</servlet-mapping>
I get the error
HTTP ERROR: 503
Problem accessing /. Reason:
Service Unavailable
I am following a tutorial and they are doing the same. I don't understand what is wrong with my code. Thanks in advance.
Ok, understood, what is happening when you are calling localhost:8080/index.htm, servlet name "dispatcher" got executed and as there is no mapping for index.html exist, you will not be able to see the index.html page.
To make it work, you have to put little more efforts.
1). Put your index.html under WEB-INF folder.
2). Create Controller:
package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
#Controller
#RequestMapping("/index")
public class IndexPageController{
#RequestMapping(method = RequestMethod.GET)
public String showIndexPage(ModelMap model) {
return "index";
}
}
3). Create mvc-dispatcher-servlet.xml as below and put it under WEB-INF folder :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.test.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".html" />
</bean>
</beans>
4). Edit your web.xml as:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
After whole day research I found the solution here
Getting Error scanning file when running jetty 9 on java 8 using the maven jetty plugin
Also I updated jetty, maven and tomcat. My current pom.xml
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>5.0.3</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-commons</artifactId>
<version>5.0.3</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
After updating everything, it is working fine now.

Jersey Maven configuration

I am trying to setup a Jersey RESTful service, the problem is that I am getting the following error when trying to do GET request to the following API: http://localhost:8080/GroupAppServer/api/status:
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
Here is my web.xml file:
<?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>GroupAppServer</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-servlet</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>GroupAppServer</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
under src I have a package called "com.groupappserver.api" which holds my Status class. Here is the code:
package com.groupappserver.api;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.groupservice.utils.Response;
#Path("/status")
public class Status
{
#GET
#Produces(MediaType.APPLICATION_JSON)
public Response getStatus()
{
return new Response();
}
}
Here is 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>GroupAppServer</groupId>
<artifactId>GroupAppServer</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.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
</dependencies>
</project>
Any ideas why I am getting this error? and how can I fix this?
Jersey servlet in your web.xml is misconfigured.
This servlet parameter should contain valid package name of your JAX-RS resource classes package:
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>GroupAppServer</param-value>
</init-param>
In your case the package name should be com.groupappserver.api and not the value GroupAppServer you've specified.
Also, you can add a new class with extends javax.ws.rs.core.Application and overwrite the method getClasses.
Put your com.groupappserver.api.Status class into the Set returned by getClasses :
package com.groupappserver.api;
#ApplicationPath("/*")
public class ApplicationConfig extends javax.ws.rs.core.Application {
private final Set<Class<?>> classes;
public ApplicationConfig() {
final Set<Class<?>> _classes = new HashSet<>();
_classes.add(Status.class);
classes = Collections.unmodifiableSet(_classes);
}
#Override
public Set<Class<?>> getClasses() {
return classes;
}
}

Categories