resteasy注入java ee bean

时间:2018-09-22 19:09:48

标签: java-ee dependency-injection resteasy

在Rest easy REST端点中,我试图从JSON对象创建新用户。为了保存新实体,我正在使用entityManager(数据源已准备就绪并存在),但是entityManager并非从Persistance上下文中注入。 EJB bean也不会注入。看起来该野蝇没有在RestEasy端点中提供任何注射。需要任何帮助来修复注射位置。

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>host.gordor</groupId>
    <artifactId>rest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <resteasy.version>3.0.14.Final</resteasy.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-servlet-initializer</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-cdi</artifactId>
            <version>${resteasy.version}</version>
        </dependency>

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
            <version>0.10.5</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
            <version>0.10.5</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
            <version>0.10.5</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

RestService(应用程序)

@ApplicationPath("/rest")
public class RestService extends Application
{
    private Set<Object> singletons = new HashSet<Object>();

    public RestService() {
        singletons.add(new BudgetRestService());
        singletons.add(new UserService());
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }
}

UserService(EndPoint)

@Path("/users")
@Produces(APPLICATION_JSON)
@Consumes(APPLICATION_JSON)
@Stateless
public class UserService {

    @Inject
    UserBean userBean;

    @PersistenceContext(unitName = "postgres")
    private EntityManager entityManager;

    @POST
    @Path("/create")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response create(User user)
    {
        entityManager.persist(user);

        String id = String.valueOf(user.getId());
        return Response.created(
                uriInfo.getAbsolutePathBuilder()
                .path(id)
                .build()
        ).build();
    }

    //another methods
}

部署描述符-jboss-deployment-structure.xml

<jboss-deployment-structure>
    <deployment>
        <exclude-subsystems>
            <subsystem name="resteasy" />
        </exclude-subsystems>
        <exclusions>
            <module name="javaee.api" />
            <module name="javax.ws.rs.api"/>
            <module name="org.jboss.resteasy.resteasy-jaxrs" />
        </exclusions>
        <local-last value="true" />
    </deployment>
</jboss-deployment-structure>

User.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "user", propOrder = {"id", "username", "password"})
@Table(name = "user")
@NamedQueries({
        @NamedQuery(name = "FIND_USER", query = "Select usr from User usr WHERE usr.username = :username and usr.password = :password"),
        @NamedQuery(name = "FIND_ALL", query = "Select usr from User usr")
})
public class User
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    public static final String FIND_USER = "FIND_USER";
    public static final String FIND_ALL = "FIND_ALL";

    private String username;
    private String password;

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }
}

beans.xml

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

1 个答案:

答案 0 :(得分:0)

一个答案是编辑RestService:

@ApplicationPath("/rest")
public class RestService extends Application
{
    private Set<Class<?>> classes = new HashSet<>();
    private Set<Object> singletons = new HashSet<Object>();

    public RestService() {
        singletons.add(new BudgetRestService());
        classes.add( UserService.class);
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }

    @Override
    public Set<Class<?>> getClasses() {
        return classes;
    }
}