[英]jersey helloworld with maven deployed on tomcat7
I want to create jersey, maven, tomcat helloworld app. 我想创建球衣,maven,tomcat helloworld应用程序。 I followed this tutorial for maven, tomcat setting and this tutorial for helloworld app creating.
我跟着这个教程的行家,tomcat的设置和本教程针对的HelloWorld应用程序创建。 My implementation can be downloaded from here , important files are below:
我的实现可以从这里下载,重要文件如下:
package com.mkyong.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/hello")
public class HelloWorldService {
@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {
String output = "Jersey say : " + msg;
return Response.status(200).entity(output).build();
}
}
<!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 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>Restful Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.mkyong.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
<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.mkyong.rest</groupId>
<artifactId>RESTfulExample</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>RESTfulExample Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
<build>
<finalName>RESTfulExample</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>TomcatServer</server>
<path>/RESTFulExample</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
When I write: mvn tomcat7:deploy everything is OK and app is deployed. 当我写时:mvn tomcat7:deploy一切正常,并部署了应用程序。 And Hello World on
localhost:8080/RESTFulExample/
is shown. 并显示
localhost:8080/RESTFulExample/
上的Hello World。 But when I go to localhost:8080/RESTFulExample
/rest/hello/something the exception: 但是当我进入
localhost:8080/RESTFulExample
/ rest / hello /时,出现了一些例外:
javax.servlet.ServletException: Servlet.init() for servlet jersey-serlvet threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
is throwed. 被抛出。 Any idea where problem can be?
知道问题可能在哪里吗? Thanks.
谢谢。
After looking through your project, the problem is with your directory structure 浏览完项目后,问题出在目录结构上
src/main/com/mkyong/rest
A Maven project has a strict project structure Maven项目具有严格的项目结构
src/
main/
java/ <===== You are missing this
com/mkyong/rest
So when you build the project, your HelloService.java
file doesn't get compiled to the required .class file. 因此,在构建项目时,您的
HelloService.java
文件不会被编译为所需的.class文件。 In a Maven project, you can look in the target/classes
, and in a webapp, in the WEB-INF/classes
. 在Maven项目中,您可以在
target/classes
和Webapp中的WEB-INF/classes
查找。 This is where the .class
files are compiled to. 这是
.class
文件被编译到的位置。 You can check there and you won't find anything. 您可以在此处检查,不会找到任何东西。
So if you add the java
directory in the correct location, and build the project again, you should see the .class
files in the correct location, and the application should work. 因此,如果将
java
目录添加到正确的位置,然后再次构建项目,则应该在正确的位置看到.class
文件,并且应用程序应该可以运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.