Maven 如何使用CDI和JAX-RS、JavaEE7配置Tomcat9?
我正在尝试实现简单的JavaREST应用程序并将其部署到Tomcat服务器。(Tomcat是必需的,所以不能选择Tomee) 这是我的项目结构: 根目录的内容Maven 如何使用CDI和JAX-RS、JavaEE7配置Tomcat9?,maven,jax-rs,cdi,java-ee-7,tomcat9,Maven,Jax Rs,Cdi,Java Ee 7,Tomcat9,我正在尝试实现简单的JavaREST应用程序并将其部署到Tomcat服务器。(Tomcat是必需的,所以不能选择Tomee) 这是我的项目结构: 根目录的内容pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&quo
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>org.example</groupId>
<artifactId>tomcat-project</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>tomcat-rest</module>
<module>tomcat-app</module>
</modules>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>META-INF/*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
TestBean.java的内容:
package org.example.rest;
import javax.ejb.Stateless;
@Stateless
public class TestBean {
public String test() {
return "Test Bean";
}
}
TestResource.java的内容:
package org.example.rest;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/test")
@Stateless
public class TestResource {
@Inject TestBean testBean;
@GET
public String test() {
return testBean.test();
}
}
正如您在tomee app pom.xml中看到的,我已经配置了maven插件tomee maven plugin
和cargo-maven2-plugin
(容器设置为tomcat9x),因为这是我发现的唯一一个运行Tomcat 9的maven插件
有了Tomee,一切正常,Jax Rs正在运行,测试bean的注入也正常工作
但有了Tomcat,我至少无法让CDI工作。我尝试了在Tomcat、OpenWebBeans、ApacheCXF等上找到的几个选项。。我总是以以太Jax-Rs结束,而不是在注入上运行,这是行不通的
我没有发布错误,因为我尝试了很多东西,结果出现了很多不同的错误
我更喜欢使用OpenWebBeans和ApacheCXF,因为我经常使用它们,但到目前为止还找不到任何工作说明
我试图按照这些方法让OpenWebBeans与Tomcat一起运行,也让Glassfish Jersey容器一起运行,但在注入TestBean时出现了错误。您需要额外的依赖项、JAX-RS实现和CDI实现 例如:
org.glassfish.jersey.containers
我将您的anwer标记为已被接受,因为它提供了有效的解决方案,但我更喜欢使用OpenWebBeans和Apache CXF的解决方案。使用OpenWebBeans,除了servlet中的依赖注入之外,我完成了所有工作,但没有找到解决方案。嗨,你试过吗?如果下载openwebbeans发行版zip,您还可以使用一个脚本进行设置:。长话短说,它需要在tomcat中进行设置,因为servlet组件由tomcat管理。另一种廉价模式是使用CDI.current()。在servlet中选择(xxxx.class),而不是@Inject。
<!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>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>
package org.example.rest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("rest")
public class RestApplication extends Application {
}
package org.example.rest;
import javax.ejb.Stateless;
@Stateless
public class TestBean {
public String test() {
return "Test Bean";
}
}
package org.example.rest;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/test")
@Stateless
public class TestResource {
@Inject TestBean testBean;
@GET
public String test() {
return testBean.test();
}
}