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
Related
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
i'm quite new to web developpement, I worked this summer on a web app on Spring, but the app was already set up, and was just doing some Java and AngularJS. But now i got some problem to start from scratch this type of application. I was trying to just test a simple web service, but can't reach the url I want, i get a 404 error. I'm working on Intellij with Tomcat 8.5.4 :
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>seb</groupId>
<artifactId>webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context">
<jpa:repositories base-package="repository" />
<mvc:annotation-driven/>
<context:component-scan base-package="controller" />
</beans:beans>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springrest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springrest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Main.java
#SpringBootConfiguration
#EnableJpaRepositories("repository")
public class Main {
public static void main (String[] args) {
SpringApplication.run(Application.class, args);
}
}
StudentController.java
#RestController
public class StudentController {
private final StudentService studentService;
public StudentController(final StudentService studentService) {
this.studentService = studentService;
}
#RequestMapping(value = "/student", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public void getSomeStudent() {
studentService.getSomeStudent();
System.out.println("Hello");
}
}
The url i'm trying to reach is : localhost:8080/webapp/student, but nothing happen, just have a 404 error. I guess i forgot something, but I looked at some tutorials and questions on the web, and can't figure how to fix my project. So if someone got some clues that can help me, that will be very helpful. Thanks by advance.
change #SpringBootConfiguration to #SpringBootApplication it configures following for you
#Configuration tags the class as a source of bean definitions for the application context.
#EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property
settings. Normally you would add #EnableWebMvc for a Spring MVC app,
but Spring Boot adds it automatically when it sees spring-webmvc on
the classpath. This flags the application as a web application and
activates key behaviors such as setting up
#ComponentScan tells Spring to look for other components, configurations, and services in the the hello package, allowing it to
find the HelloController.
Since you are using spring-boot, add the following dependency,and remove web.xml where spring-boot automatically configures web dispatcher servlet for you
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
or you can still go ahead and configure one if you want.
Based on your initial settings, controller should be located at
http://localhost:8080/student (no 'webapp')
Well I stopped trying to deploy my app on tomcat, i'm just using spring now and i can reach my url. Anyway i will take deeper look at spring docs and tutorials, thanks for the different explications and link.
Use https://start.spring.io/ to create your Spring Boot Application project.
This will generate spring boot maven project for you.
Then add your StudentController there.
and afer that as #jahra specified, you have to trigger URL : http://localhost:8080/student to access your student webservice
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; }
}
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.
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>