Search code examples
jakarta-eedependency-injectionresteasy

resteasy inject java ee bean


In Rest easy REST endpoint i'm trying to create new user from JSON object. To save new entity i'm using entityManager( datasource is ready and exists), but entityManager doesn't injected from Persistance context. EJB beans doesn't inject too. This looks like that wildfly not provide any injection in RestEasy end point. Need any help to fix injection where.

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(Application)

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

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

Solution

  • an answer was editing 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;
        }
    }