java—如何正确地将entitymanager注入jax ws web服务?

lyfkaqu1  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(159)

我试图实现一个jax-ws-web服务,它有一个从配置的数据源返回数据的端点。我成功地创建了服务和客户机,但是我无法访问数据库,因为当我尝试使用注入的entitymanager时,会出现空指针异常。这是我的服务:

@WebService(serviceName="ViewDocuments")
public class ViewDocumentService {

    @PersistenceContext(unitName = "HibernateJPA")
    private EntityManager em;

    @WebMethod
    public String sayHello() {
        return "Hello world!";
    }

    @Produces("application/json")
    @WebMethod(operationName="getDocuments")
    public List<File> getDocuments(@WebParam(name = "name") String filename) {
        if(filename == null)
            return em.createQuery("FROM File").getResultList();
        else return null;
    }
}

这是我的webserviceclient:

@WebServiceClient(name="ViewDocuments", wsdlLocation = "http://localhost:8080/lab8/ViewDocuments?wsdl")
public class ViewDocumentClient extends Service {

    protected ViewDocumentClient(URL wsdlDocumentLocation, QName serviceName) {
        super(wsdlDocumentLocation, serviceName);
    }

    @WebEndpoint(name = "DocumentPort")
    public ViewDocumentService getDocumentPort() {
        return new ViewDocumentService();
    }
}

我试图在这里调用函数:

public class MainDocumentClient {
    public static void main(String[] args) throws Exception {
        URL wsdlUrl = new URL("http://localhost:8080/lab8/ViewDocuments?wsdl");
        QName serviceName = new QName("http://webservices/", "ViewDocuments");
        ViewDocumentClient service = new ViewDocumentClient(wsdlUrl, serviceName);
        ViewDocumentService viewDocumentService = service.getDocumentPort();
        System.out.println(viewDocumentService.sayHello());
        System.out.println(viewDocumentService.getDocuments(null));
    }
}

这是我的persistence.xml文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
     http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">

    <persistence-unit name="HibernateJPA" transaction-type="JTA">

        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <jta-data-source>java:/Postgres-Source8</jta-data-source>

        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.archive.autodetection" value="class, hbm" />

        </properties>

    </persistence-unit>

    <persistence-unit name="HibernateJPAForTests" transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/lab8" />
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.user" value="postgres" />
            <property name="javax.persistence.jdbc.password" value="<password>" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.archive.autodetection" value="class, hbm" />

        </properties>

    </persistence-unit>
</persistence>

这个 sayHello() 这个方法很有效。我还试着用 EntityManagerFactory ,以示例化我的 Repository 手工添加 @Stateless 注解,但似乎什么都不起作用。我只是无法查询我的数据库。经过大量的研究,我似乎仍然不能理解如何正确地注入entitymanager。有人能给我解释一下吗?
我正在使用wildfly21.0.0。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题