我是Jersey的新手,并试图使用Tomcat和eclipse建立一个基本的webapp。我查看了很多教程和示例,但它们彼此不同,或省略了部分过程(例如使用Spring,使用web.xml文件,部署非Jersey应用程序) Tomcat等。)。
这是我到目前为止的地方:
我在eclipse中创建了一个名为TestProject的动态Web项目,并将其转换为maven项目。
这是我的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>test</groupId>
<artifactId>test</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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jersey.version>2.23.1</jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<!-- POJO to JSON support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- required to run without web.xml on Tomcat -->
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
我有一个包{3}},其中包含3个类:
com.test
创建没有web.xml的应用程序:
TestApplication
@ApplicationPath("/*")
public class TestApplication extends ResourceConfig {
public TestApplication(){
packages("com.test");
register(JacksonFeature.class);
}
}
作为最小资源类:
TestProvider
@Path("/test")
public class TestProvider {
@GET
@Produces("application/json")
public Test getTest(){
return new Test();
}
}
作为以JSON格式返回的最小对象:
Test
我有一个运行Tomcat服务器,项目已添加为Web模块。如果我运行项目(运行为&gt;在服务器上运行),并导航到public class Test {
private String field = "test";
}
或http://localhost:8080/test
我会得到一个标准的tomcat 404。
这是我的Tomcat启动日志:
http://localhost:8080/TestProject/test
如果我使用注释jun 14, 2016 11:29:06 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:TestProject' did not find a matching property.
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/8.0.35
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: May 11 2016 21:57:08 UTC
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 8.0.35.0
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 7
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 6.1
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Program Files\Java\jre1.8.0_71
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_71-b15
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: C:\Users\tim\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: C:\Users\tim\workspace\apache-tomcat-8.0.35
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\tim\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Users\tim\workspace\apache-tomcat-8.0.35
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\tim\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Users\tim\workspace\apache-tomcat-8.0.35\endorsed
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
jun 14, 2016 11:29:06 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_71\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:/Program Files/Java/jre1.8.0_71/bin/server;C:/Program Files/Java/jre1.8.0_71/bin;C:/Program Files/Java/jre1.8.0_71/lib/amd64;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\ImageMagick-6.9.1-Q16;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\ProgramFiles\apache-maven-3.2.5\bin;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\eclipse;;.
jun 14, 2016 11:29:06 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
jun 14, 2016 11:29:06 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
jun 14, 2016 11:29:06 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
jun 14, 2016 11:29:06 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 941 ms
jun 14, 2016 11:29:06 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
jun 14, 2016 11:29:06 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.35
jun 14, 2016 11:29:07 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [187] milliseconds.
jun 14, 2016 11:29:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
jun 14, 2016 11:29:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
jun 14, 2016 11:29:07 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 668 ms
创建非球衣HttpServlet,我会在@WebServlet(name="hello",urlPatterns={"/hello"})
得到回复。
我错过了什么?此外,上述任何一种不是最佳实践&#39;对于基本的Web应用程序?看来这项技术变化很快,很难知道哪些例子或方法已经过时。
答案 0 :(得分:1)
Jersey 2 Tomcat 8,Java 8&amp; Servlet 3.0基本最小配置
http://localhost:{port}访问tomcat管理器。您可以登录以查找上下文路径。
访问您的申请:
http://localhost:{端口} / {上下文路径} / {URL模式} / {资源路径}
<强>的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>jersey2</groupId>
<artifactId>jersey2</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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jersey.version>2.23.1</jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
<强>应用强>
package com.test;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("/*")
public class TestApplication extends ResourceConfig {
public TestApplication(){
packages("com.test");
register(JacksonFeature.class);
}
}
<强>资源强>
package com.test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/test")
public class TestResource {
@GET
@Produces("application/json")
public Test getTest(){
Test bean = new Test();
bean.setName("Jersey");
bean.setVersion("2.0");
bean.setServlet("3.0");
return bean;
}
}
测试Bean
package com.test;
public class Test {
private String name;
private String version;
private String servlet;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getServlet() {
return servlet;
}
public void setServlet(String servlet) {
this.servlet = servlet;
}
}
答案 1 :(得分:0)
添加答案,如果你使用的是杰克逊,那么依赖已被提及为jersey-media-json-jackson -
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
还有其他json提供商。
如果要将MOXy用作JAXB实现,则可以使用
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>${jersey.version}</version>
</dependency>
要使用JSON-P作为JSON提供程序,您需要添加
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
<version>${jersey.version}</version>
</dependency>
要将Jettison用作JSON提供程序,您需要将jersey-media-json-jettison模块添加到pom.xml文件中:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jettison</artifactId>
<version>${jersey.version}</version>
</dependency>
要使用多部分功能,您需要将jersey-media-multipart模块添加到pom.xml文件中:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
更多详情,请参阅https://jersey.java.net/documentation/latest/media.html