gpt4 book ai didi

java - eclipse 中 Tomcat 上 Jersey 的基本完整配置

转载 作者:行者123 更新时间:2023-11-28 22:17:47 27 4
gpt4 key购买 nike

我是 Jersey 的新手,正在尝试使用 Tomcat 和 Eclipse 设置一个基本的 Web 应用程序。我看过很多教程和示例,但它们彼此都不相同,或者省略了一部分过程(例如,使用 Spring,使用 web.xml 文件,在 Tomcat 上部署非 Jersey 应用程序等。 ).

这是我到目前为止的位置:

我在 eclipse 中创建了一个名为 TestProject 的动态 Web 项目,并将其转换为一个 maven 项目。

server modules

这是我的 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 个类的包 com.test:

TestApplication 创建一个没有 web.xml 的应用程序:

@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 格式返回的最小对象:

public class Test {

private String field = "test";

}

我有一个运行的 Tomcat 服务器,项目添加为 Web 模块。如果我运行项目(运行方式 > 在服务器上运行),并导航到 http://localhost:8080/testhttp://localhost:8080/TestProject/test 我得到一个标准的 tomcat 404。

这是我的 Tomcat 启动日志:

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

如果我用注解 @WebServlet(name="hello",urlPatterns={"/hello"}) 创建一个非 jersey HttpServlet,我会在 http 得到响应://localhost:8080/TestProject/hello.

我错过了什么?此外,以上任何一项都不是基本 Web 应用程序的“最佳实践”吗?看起来这项技术变化很快,很难知道哪些例子或方法已经过时了。

最佳答案

Jersey 2 Tomcat 8、Java 8 和 Servlet 3.0 基本最低配置

http://localhost :{port} 访问tomcat管理器。您可以登录以查找您的上下文路径。

在以下位置访问您的应用程序:

http://localhost :{port}/{context-path}/{url-pattern}/{resource-path}

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;
}
}

关于java - eclipse 中 Tomcat 上 Jersey 的基本完整配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37778893/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com