2

我正在尝试运行 ResteasyApplication。我的主要问题是实施 CDI,这应该不太难。

所以这是我的问题。我@NamedService课堂上和课堂@Inject上使用RestService

@Path("/patient")
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON 

}) 
@Consumes    
@Stateless
public class PatientREST {


    @Inject
    PatientService patientService;...

@Named
public class PatientService {

    // Resteasy demands a no-arg constructor
    public PatientService() {
    }


    public Patient getPatient(String patientNumber) {...

我的 beans.xml 看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_1_0.xsd">
</beans>

整个过程中,我都尝试像几个 TomEE-HowTos 描述的那样使用它。

当我尝试在我的 TomEE (1.5.2) 上部署它时,我在堆栈跟踪中得到了它。

uses @Inject but CDI is not enabled. Maybe youd need to add a beans.xml file.


javax.enterprise.inject.UnsatisfiedResolutionException: Api type [de.klinikum.service.PatientService] is not found with the qualifiers 
Qualifiers: [@javax.enterprise.inject.Default()]
for injection into Field Injection Point, field name :  patientService, Bean Owner : [PatientREST, Name:null, WebBeans Type:ENTERPRISE, API Types:[java.lang.Object,de.klinikum.communication.PatientREST,java.io.Serializable], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]]

任何想法?

我尝试了更多......现在......部署工作。但我仍然收到由空指针引起的 HTTP500 失败

type Exception report

message java.lang.NullPointerException

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
    org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:365)
    org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:233)
    org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:209)
    org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:557)
    org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:524)
    org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:126)
    org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208)
    org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55)
    org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

java.lang.NullPointerException
    de.klinikum.communication.PatientREST.getPatient(PatientREST.java:48)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:167)
    org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:269)
    org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:227)
    org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:216)
    org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:542)
    org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:524)
    org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:126)
    org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208)
    org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55)
    org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat (TomEE)/7.0.37 logs.

似乎容器无法提供 PatientService bean……但为什么呢?

.----------------- 仍然得到相同的失败.. 也许我需要一些特殊的依赖项?...这是我的 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>de.klinikumlmu.server</groupId>
    <artifactId>KlinikumServer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>KlinikumServer</name>
    <properties>
        <sesame.version>
            2.7.0
        </sesame.version>
    </properties>
    <packaging>war</packaging>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <webXml>WebContent\WEB-INF\web.xml</webXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.openejb.maven</groupId>
                <artifactId>tomee-maven-plugin</artifactId>
                <version>1.5.2</version>
                <configuration>
                    <tomeeVersion>1.5.2</tomeeVersion>
                    <!-- <tomeeClassifier>plus</tomeeClassifier> -->
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
            <dependency>
      <groupId>org.apache.openejb</groupId>
      <artifactId>javaee-api</artifactId>
      <version>6.0-4</version>
      <scope>provided</scope>
    </dependency>
        <dependency>
            <groupId>org.openrdf.sesame</groupId>
            <artifactId>sesame-runtime</artifactId>
            <version>${sesame.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>catalina</artifactId>
            <version>6.0.26</version>
        </dependency>
        <dependency>
    <groupId>org.apache.openejb</groupId>
    <artifactId>cdi-tomee</artifactId>
    <version>4.5.2</version>
</dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>2.3.6.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxb-provider</artifactId>
            <version>2.3.6.Final</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.10</version>
            </plugin>
        </plugins>
    </reporting>
</project>

我越来越深入。我试图将 beans.xml 放在 WEB-INF 文件夹和 META-INF 文件夹中以...

4

2 回答 2

1

我们得到了答案^^..

要将 CDI 与 Resteasy 和 TomEE 一起使用,您必须包含特殊的 resteasy cdi 依赖项。由于我们使用它,所以一切正常...

<dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-cdi</artifactId>
            <version>${resteasy.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.jboss.resteasy</groupId>
                    <artifactId>resteasy-jaxrs</artifactId>
                </exclusion>
            </exclusions>
</dependency>

感谢我们的帮助...

于 2013-05-15T09:52:43.377 回答
0

您的 REST 端点需要@RequestScoped启用 CDI 注入。此外,您的注入点应该是

@Inject @Named("PatientService") PatientService patientService;

既然你有@Named服务。

顺便说一句,没有 TomEE 7 这样的东西。有 TomEE 1.5.x 和 Tomcat 7.0。我假设您使用的是 TomEE 1.5.x

于 2013-05-06T14:49:46.190 回答