I am trying to make an example of using jersey 2 + spring 4 + jetty-maven-plugin. But keep getting this error, cannot understand why.. Please give me a hand.
WARNING: The Jersey servlet application, named com.joejag.code.orders.restservices.ResourceConfiguration, is not annotated with ApplicationPath and has no servlet mapping.
2015-12-16 19:56:38.746:INFO:/.0-SNAPSHOT:main: Spring WebApplicationInitializers detected on classpath: [org.glassfish.jersey.server.spring.SpringWebApplicationInitializer@2776015d]
2015-12-16 19:56:38.778:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.m.p.JettyWebAppContext@15fb7a32{/orders-server-1.0-SNAPSHOT,file:///home/bryan-1/workspace/project/java/simple-java-restful-service-using-jersey-and-maven-master/src/main/webapp/,STARTING}{file:///home/bryan-1/workspace/project/java/simple-java-restful-service-using-jersey-and-maven-master/src/main/webapp/}
java.lang.IllegalStateException: No such servlet: Example
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.joejag.code.orders</groupId>
<artifactId>orders-server</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Example</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<jersey.version>2.22.1</jersey.version>
</properties>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>https://maven.java.net/content/groups/public/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<!-- Jersey 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>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Jersey + Spring -->
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring4</artifactId>
<version>3.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.6.v20151106</version>
<configuration>
<scanTargets>
<scanTarget>${project.basedir}/src/main</scanTarget>
<scanTarget>${project.basedir}/src/test</scanTarget>
</scanTargets>
<webAppConfig>
<contextPath>/${project.artifactId}-${project.version}</contextPath>
</webAppConfig>
<contextPath>${project.artifactId}</contextPath>
</configuration>
</plugin>
</plugins>
</build>
</project>
My 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">
<display-name>Example</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.joejag.code.orders.restservices.ResourceConfiguration</param-value>
</init-param>
<!-- <init-param> <param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.joejag.code.orders.restservices</param-value> </init-param> -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Example</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
My ResourceConfig class
package com.joejag.code.orders.restservices;
import org.glassfish.jersey.server.ResourceConfig;
public class ResourceConfiguration extends ResourceConfig {
public ResourceConfiguration() {
register(OrdersService.class);
}
}
My applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.joejag.code.orders.restservices" />
<bean id="transactionBo" class="TransactionBoImpl" />
<!-- <bean class="OrderService" /> -->
</beans>
EDITED
after fixing inconsistant servlet-name
issue Jetty is able to load the servlet, but a new issue arise that all the @PUT
method seems to no longer execute. Here the OrderService look like.
package com.joejag.code.orders.restservices;
import java.util.Map;
import java.util.TreeMap;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
// * curl -X PUT http://127.0.0.1:8080/orders-server/orders/1?customer_name=bob
// * curl -X GET http://127.0.0.1:8080/orders-server/orders/1
* curl -X GET http://127.0.0.1:8080/orders-server/orders/list
*/
@Component
@Path("orders")
public class OrdersService
{
public static Map<String, String> orders = new TreeMap<String, String>();
//@Autowired
//private TransactionBo transactionBo;
public OrdersService()
{
//his.transactionBo = transactionBo;
}
@Path("/{order}")
@PUT
@Produces("text/html")
public String create(@PathParam("order") String order, @QueryParam("customer_name") String customerName)
{
orders.put(order, customerName);
return "Added order #" + order ;//+ this.transaction.save();
}
@Path("/{order}")
@GET
@Produces("text/html")
public String find(@PathParam("order") String order)
{
if (orders.containsKey(order))
return "<h2>Details on Order #" + order + "</h2><p>Customer name: " + orders.get(order);
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
@Path("/list")
@GET
@Produces("text/html")
public String list()
{
String header = "<h2>All Orders</h2>\n";
header += "<ul>";
for (Map.Entry<String, String> order : orders.entrySet())
header += "\n<li>#" + order.getKey() + " for " + order.getValue() + "</li>";
header += "\n</ul>";
return header;
}
}