resteasy 注入 java ee bean
resteasy inject java ee bean
在 Rest easy REST 端点中,我正在尝试从 JSON 对象创建新用户。为了保存新实体,我正在使用 entityManager(数据源已准备好并存在),但 entityManager 并未从 Persistance 上下文中注入。 EJB bean 也不会注入。这看起来像 wildfly 没有在 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>
休息服务(应用程序)
@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;
}
}
用户服务(端点)
@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-部署-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>
一个答案正在编辑 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;
}
}
在 Rest easy REST 端点中,我正在尝试从 JSON 对象创建新用户。为了保存新实体,我正在使用 entityManager(数据源已准备好并存在),但 entityManager 并未从 Persistance 上下文中注入。 EJB bean 也不会注入。这看起来像 wildfly 没有在 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>
休息服务(应用程序)
@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;
}
}
用户服务(端点)
@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-部署-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>
一个答案正在编辑 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;
}
}