Getting all of it to work: War, OSGI, Spring Beans, Maven - maven

Trying to deploy a war with a bean file in a Fuse Servicemix (version 4.3.1). I'm using maven to build my war. I can't seem to get this to work. Can anyone provide a website that can tell me how to do this?
This website tells me what to put in the web.xml file but doesn't explain the rest.
http://fusesource.com/docs/esbent/7.0/esb_deploy_osgi/BuildWar-Spring.html.
I've tried several solutions and methods over the course of 19 days. Everyone seems to skin this cat differently but none of them work for me.
fat war (SOLVED):
See answer below
skinny war:
Seems impossible in osgi. Need to manually import too many packages.
This link appears to solve it but seems there a lot of nasty side effects.
http://davidvaleri.wordpress.com/2011/08/17/deploying-spring-mvc-based-web-applications-to-osgi-using-apache-servicemix/

You need to add the Spring OSGi ContextLoaderListener to your web.xml otherwise it doesn't work. You'll also need dependencies to Spring-DM 1.2.1.
Take a look at Pax Web Spring sample and especially the web.xml in it. It's a working example on how to use Spring in Karaf / Fuse-ServiceMix ...
I guess I pointed you to the wrong sample. You need t use the following.
contextClass
org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext

Fat War solution
This is the minimum viable solution that worked for me. I played around trying to remove things and it broke as soon as I did, often without even posting an error message.
directory structure:
src/main/java/test/Test.java
src/main/webapp/WEB-INF/web.xml
src/main/webapp/WEB-INF/applicationContext.xml
pom.xml
...
<groupId>test</groupId>
<artifactId>war-bean-test</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.osgi</groupId>
<artifactId>spring-osgi-web</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<supportedProjectTypes>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
<supportedProjectType>war</supportedProjectType>
</supportedProjectTypes>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<!-- IMPORTANT resolution:=optional fixes bug where bundle fails to load unnecessary packages such as bsh. You also need javax.servlet. In Servicemix 4.3.1 it is provided by geronimo servlet. -->
<Import-Package>
javax.servlet
*; resolution:=optional
</Import-Package>
<Export-Package></Export-Package>
<!-- IMPORTANT explicitly adding the jars fixes the numerous CassNotFoundExceptions -->
<Bundle-ClassPath>
.,WEB-INF/classes,{maven-dependencies}
</Bundle-ClassPath>
<Web-ContextPath>warbeantest</Web-ContextPath>
<Webapp-Context>warbeantest</Webapp-Context>
<!-- adding inline=true to Embed-Dependency causes {maven-dependencies} to not work and you will have to add every jar by hand -->
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Embed-Directory>WEB-INF/lib</Embed-Directory>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee">
<display-name>war-bean-test</display-name>
<description>war-bean-test</description>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- If you remove this then the spring beans will still work, but you wont be able to fetch services and resources from other osgi bundles -->
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.0.xsd">
<bean id="test" class="test.Test">
<property name="value" value="1" />
</bean>
</beans>
Test.java
package test;
public class Test {
private int value = 0;
public TestImpl() { }
public void setValue(int value) {
// Should print to console when you load into Fuse Servicemix
System.out.println("testing...");
this.value = value;
}
public int getValue() { return value; }
}

Related

404 error springboot restcontroller requestmapping

I have a simple web application with springboot (netbeans 8.2), but resource mapping does not work, I have 2 days with the problem. any ideas?
ApplicationContext://///////////////////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/springXML8"/>
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>example</display-name>
<absolute-ordering />
<servlet>
<servlet-name>ServletCentral</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletCentral</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
ServletConfig:////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- paquete a escanear en busca de componentes -->
<mvc:annotation-driven/>
</beans>
Application://////////////////////////////////////////////////////////////
#SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Controller:///////////////////////////////////////////////////////////////
#RestController
public class controller1 {
#RequestMapping("/")
public String hello(){
return "Lain love this service";
}
}
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>
<groupId>beans</groupId>
<artifactId>beans</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<name>SpringXML8</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Ok.
In itself what is your problem? How u are testing? Are you using postman, soap ui?
Based on your mapping your "url-pattern"/* "url-pattern" in web.xml is empty, so you donĀ“t need anymore in your URL Request. But you have little issues. Why do you have two Spring appCtx (ApplicationContext & ServletConfig)??
You do not provide more information. I don't know if you are trying to raise a Tomcat server or simple spring boot server default. You could stay just with ServletConfig, you shouldn't have -Context path="/springXML8"-
You could replace this with a #ResquestMapping global in your RestController. i.e.
#RestController
#RequestMapping("initialLoginOrSpringXML8")
public class PersonalController {
#RequestMapping("/getHello")
public String hello(){
return "Lain love this service";
}
}
Besides I suggest to add some word in your local #RequestMapping by method.
Remember if you use #RequestParams or #PathVariable, you must enter them especially depending on the test client you are using. As well as the type of request Http
#Diego Caballero
There are many options for which you have this error. The most common is a bad mapping of the URL.
The server may be confused by the 2 settings, it may not know which context path to respect. Verify the URL you are using, try different options, combinations.
Change
<url-pattern>/*</url-pattern>
to
<url-pattern>/</url-pattern>
Currently, your mapped DispatcherServlet is marked as handling all requests because of /*. This means that it will also attempt to handle a request dispatched to /WEB-INF/*** It obviously doesn't have a handler for that and will fail.
The pattern / is special in that it is the default fallback if no other pattern matches. If there's a Servlet that maps to the request path, that Servlet will be chosen before your mapped DispatcherServlet. (This maybe is your case).
Try to log everything you can. Use level of info, error and debug, of log4j. Also try to read the server logs (this is located in the folder where you downloaded it). Not only the console of your actual IDE.
Verify if you are deploying correctly the application. Maven cycle, etc. WAR or JAR?
Are you using the correct domain? localhost? or maybe is an ip or a diffente domain setted in the server options deploy
I hope it was a little help. Regards

Cannot run simple servlet on GlassFish-4

My simple REST based webservice does encounter a problem when I try do run it. It seems that the deployment succeeds, but when I access the servlet I get this error:
WARNING: StandardWrapperValve[MySimpleServer]: Servlet.service() for servlet MySimpleServer threw exception
java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;
at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:662)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
My root pom.xml is very simple and just contains these dependencies and plugins:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
<build>
<finalName>MySimpleServer</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
<version>3.1</version>
</plugin>
</plugins>
</build>
The 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"
version="3.0">
<servlet>
<servlet-name>MySimpleServer</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MySimpleServer</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Actually, it worked earlier today and I have not made any changes at all to the pom.xml file. Is it something with Glassfish4 server? I have made a clean build, redeployed the servlet, restarted GlassFish etc. Nothing helps.
I've had this problem when I've accidentally mixed Jersey / JAX-RS versions - IE having both Jersey 1 & 2 libs on the classpath.
I can't see how this happens with just the three dependencies in your pom, but I'm quite sure this is happening somewhere.
Looking at your stacktrace, I can see this is what's happening.
This is javax.ws.rs-api/2.0 UriBuilder.java
118 public static UriBuilder fromUri(String uriTemplate) {
119 return newInstance().uri(uriTemplate);
120 }
Which is exactly what you're getting - a call to UriBuilder.uri(String) on line 119.
Looking at the equivalent line numbers in the UriBuilder source from Jersey 1.8 and it's a Javadoc comment for a different method.
So, you've somehow got jax-rs 2 on your classpath and it's causing this problem.
Maybe you have it on a shared / common classloader, or a stray lib lying around.
UPDATE
Yes - glassfish ships with Jersey / JAX-RS libs.
See this question
As an aside - if you're able, I'd upgrade to Jersey 2.
Hope this helps
Will

How to package EAR for GlassFish with Maven?

I want to package one EAR that will be deployed on GlassFish Server Open Source Edition.
Here are the relevant parts of the pom.xml file.
<?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>ear</packaging>
<dependencies>
....
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<configuration>
<version>6</version>
<defaultLibBundleDir>/lib</defaultLibBundleDir>
</configuration>
</plugin>
</plugins>
</build>
</project>
I usually run mvn compile and mvn package in the command terminal. The resulting EAR has the following structure.
EAR/lib/*.jar
EAR/META-INF/application.xml
EAR/META-INF/META-INF.MF
EAR/META-INF/maven/...
The application.xml is
<?xml version="1.0" encoding="UTF-8"?>
<application 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/application_6.xsd" version="6">
<display-name>test-app</display-name>
<library-directory>/lib</library-directory>
</application>
If I try to run asadmin deploy test-app.ear to deploy the EAR to GlassFish I get this error.
remote failure: Error occurred during deployment: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 22; Deployment descriptor file META-INF/application.xml in archive ....
Here I rename application.xml to glassfish-application.xml and change its content to
<!DOCTYPE glassfish-application PUBLIC "-//GlassFish.org//DTD
GlassFish Application Server 3.1 Java EE Application 6.0//EN"
"http://glassfish.org/dtds/glassfish-application_6_0-1.dtd">
<glassfish-application>
<unique-id>67488732739338240</unique-id>
</glassfish-application>
If I rerun asadmin deploy test-app.ear GlassFish recognizes the deployment descriptor but throws the next error that says Application [test-app] contains no valid components.
Here I move all jars from EAR/lib/*.jar to EAR/META-INF/lib/*.jar.
If I now rerun asadmin deploy test-app.ear GlassFish recognizes the EAR as valid and deploys it.
Since I dont want to manually change the EAR every time. How can I configure Maven to
1. Output a valid application.xml or glassfish-application.xml
2. Copy the dependencies not to EAR/lib/ but to EAR/META-INF/lib (if it is really necessary)
Thanks in advance.
How can I configure Maven to
Output a valid application.xml or glassfish-application.xml
Copy the dependencies not to EAR/lib/ but to EAR/META-INF/lib (if it is really necessary)
application.xml can be autogenerated by maven-ejb-plugin and for the simple test I would leave it up to plugin
for dependencies copying - it depends what you package in your ear (can be war/jar/...) but in general, it's a good idea, to let maven do it. For the purpose you miss in your pom.xml sections that would refer to modules (war/jar/...) you want to be included in there
moreover I don't see a reason for non-standard libs folder you specified with: <library-directory>
So I'd go for config like the sample present here.
To include the relevant sections in answer:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<configuration>
<version>6</version>
<modules>
<webModule>
<groupId>com.mycompany</groupId>
<artifactId>myWar</artifactId>
<bundleFileName>myWarNameInTheEar.war</bundleFileName>
<contextRoot>/myWarConext</contextRoot>
</webModule>
<ejbModule>
<groupId>com.mycompany</groupId>
<artifactId>myEjb</artifactId>
<bundleFileName>myEjbNameInTheEar.jar</bundleFileName>
</ejbModule>
</modules>
<displayName>My Ear Name displayed in the App Server</displayName>
<generateApplicationXml>true</generateApplicationXml>
</configuration>
</plugin>
</plugins>
</build>
<!-- Define the versions of your ear components here -->
<dependencies>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>myWar</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>myEjb</artifactId>
<version>1.0-SNAPSHOT</version>
<type>ejb</type>
</dependency>
</dependencies>
Please note you need to specify dependencies - for modules, but include those in modules section as well, to have them packaged.
Feel free to ask in case of any further questions.

Errors while calling mongo-jackson mapper from RESTEasy method

Been on this one for a few days now. I have a java servlet, built with maven that will be deployed to Jetty (an older version). It's a RESTful web service on Jetty built with RESTEasy and Jackson, and the Jackson Mongo Mapper to connect me to MongoDB.
I can run the application from maven/jetty just fine (using mvn jetty:run), and it returns JSON as expected for queries that don't use the Jackson Mongo Mapper/Jackson bit. When I send a request that triggers Jackson and the mapper, however, I first get this error:
java.lang.NoClassDefFoundError: org/codehaus/jackson/map/deser/std/StdDeserializer
When I submit a second time (and all subsequent requests), I get this error:
java.lang.NoClassDefFoundError: Could not initialize class net.vz.mongodb.jackson.JacksonDBCollection
As nearly as I can tell, I have all the dependencies set up correctly, although I'll include my web.xml and pom.xml at the end of the question. If it isn't a dependency, it's occurred to me that there might be some issue with how my bean (BillItem.class) is getting passed. I'm relatively new to Java so this could easily be a stupid mistake rather than something related to the specific stack I'm trying to implement...any ideas of what is going on?
Here's 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_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!--
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param> -->
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.myproject.BillServer</param-value>
</context-param>
<context-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.myproject.Service</param-value>
</context-param>
<context-param>
<param-name>resteasy.resource.method-interceptors</param-name>
<param-value>org.jboss.resteasy.core.ResourceMethodSecurityInterceptor</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Here's my pom.xml:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myproject</groupId>
<version>0.0.1-SNAPSHOT</version>
<name>MyProject</name>
<artifactId>MyProject</artifactId>
<packaging>jar</packaging>
<properties>
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.1.7.v20120910</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1-glassfish</artifactId>
<version>2.1.v20100127</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>2.3.4.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.4.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>2.3.4.Final</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.9</version>
</dependency>
<dependency>
<groupId>net.vz.mongodb.jackson</groupId>
<artifactId>mongo-jackson-mapper</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- The maven app assembler plugin will generate a script that sets up the classpath and runs your project -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<assembleDirectory>target</assembleDirectory>
<programs>
<program>
<mainClass>com.MyProject.Main</mainClass>
<name>webapp</name>
</program>
</programs>
<useAllProjectDependencies>true</useAllProjectDependencies>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And the last line of the following is the offending call that's throwing the error:
Mongo mongo = new Mongo(MONGO_PATH, MONGO_PORT);
DB db = mongo.getDB(MONGO_APPDB);
DBCollection collection = db.getCollection(MONGO_BILL_COL);
JacksonDBCollection<BillItem, String> coll = JacksonDBCollection.wrap(collection, BillItem.class, String.class);
This was some serious idiocy. I accidentally deleted 3 of the jackson dependencies when I was cleaning up my pom file.

404 Not Found Error in a simple Jetty/Maven Hello World webapp

I have followed the instructions to create a "Standard WebApp with Jetty and Maven" precisely as described on the eclipse wiki: http://wiki.eclipse.org/Jetty/Tutorial/Jetty_and_Maven_HelloWorld#Developing_a_Standard_WebApp_with_Jetty_and_Maven
However when I run the webapp (mvn jetty:run) and go to localhost:8080/hello-world/hello I end up at a "HTTP ERROR 404 Problem accessing /hello-world/hello. Reason: Not Found". I have read through documentation, looked at the wiki page's history, poked around other forums and stackoverflow threads, but can not find the answer to this seemingly simple problem. I will post my source, but it is literally the same as the tutorial.
Any help would be appreciated. I'd really like to start playing around with this technology but its disheartening to keep slamming into the same dead end.
(Please note: the first part of the tutorial to create "JettyMavenHelloWorld" work fine. My problem is with the second part, "JettyMavenHelloWarApp". This section is titled "Developing a Standard WebApp with Jetty and Maven")
JettyMavenHelloWarApp/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>org.example</groupId>
<artifactId>hello-world</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Jetty HelloWorld</name>
<properties>
<jettyVersion>7.2.0.v20101020</jettyVersion>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jettyVersion}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- This plugin is needed for the servlet example -->
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jettyVersion}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution><goals><goal>java</goal></goals></execution>
</executions>
<configuration>
<mainClass>org.example.HelloWorld</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
JettyMavenHelloWarApp/src/main/java/org/example/HelloServlet.java
package org.example;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>Hello Servlet</h1>");
response.getWriter().println("session=" + request.getSession(true).getId());
}
}
JettyMavenHelloWarApp/src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>org.example.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello/*</url-pattern>
</servlet-mapping>
</web-app>
JettyMavenHelloWarApp/src/main/webapp/index.html
<h1>Hello World Webapp</h1>
Hello Servlet
The tutorial gives the incorrect url - your app context is still "/",
so the urls are http://localhost:8080 and http://localhost:8080/hello for the static and dynamic content, respectively.
The maven jetty plugin documentation does claim that the default context will be named the same as the artifactId in the pom.xml, but that doesn't seem to be working here.
I ran into the same issue and what worked for me was accessing the app like: http://localhost:8080/hello/index.jsp or http://localhost:8080/hello/index.html, whatever you're using html or js pages.
I think adding configuration to the jetty plugin definition in your pom should change the contextpath to hello-world:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jettyVersion}</version>
<configuration>
<webApp>
<contextPath>/hello-world</contextPath>
</webApp>
</configuration>
</plugin>
This is based on jetty version 9. See http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin for the configuration options.
I had the same problem with a basic configuration.
I wanted to redirect with Spring 3 MVC an error page with this configuration (in web.xml)
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/error.html</location>
</error-page>
I solve it by changing extension of error.html to error.jsp.
I think you don't run mvn package task before mvn jetty:run that is why jetty doesn't see any sources. Just run mvn package first.
Your servlet mapping is incorrect or insufficient.
<url-pattern>/hello/*</url-pattern> // does not respond to "/hello"
You need to add a mapping for the URL pattern "/hello"
<url-pattern>/hello</url-pattern>

Resources