我正在尝试在WildFly Swarm中运行一个简单的实体 - 控制器 - boundery应用程序。这样可以正常工作,但是当我添加ExceptionMapper
以捕获NotFoundException
时,这可以在WildFly服务器10上运行,但不能在WildFly群中运行。这是Shrinkwrap中的一个错误吗?这是Wildfly Swarm中的一个错误吗?或者我在Shrinkwrapping部署中做错了什么?
这里是Swarm Main类的具体代码:
public class Main {
public static void main(String[] args) throws Exception {
Container container = new Container();
container.fraction(new DatasourcesFraction()
.jdbcDriver("h2", (d) -> {
d.driverClassName("org.h2.Driver");
d.xaDatasourceClass("org.h2.jdbcx.JdbcDataSource");
d.driverModuleName("com.h2database.h2");
})
.dataSource("demoDS", (ds) -> {
ds.driverName("h2");
ds.connectionUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
ds.userName("sa");
ds.password("sa");
})
);
// Prevent JPA Fraction from installing it's default datasource fraction
container.fraction(new JPAFraction()
.inhibitDefaultDatasource()
.defaultDatasource("jboss/datasources/demoDS")
);
container.start();
JAXRSArchive deployment = ShrinkWrap.create(JAXRSArchive.class);
deployment.addClasses(Customer.class);
deployment.addAsWebInfResource(new ClassLoaderAsset("META-INF/persistence.xml", Main.class.getClassLoader()), "classes/META-INF/persistence.xml");
deployment.addClass(CustomerService.class);
deployment.addResource(CustomerResource.class);
//BUG: The provider below is not working!
deployment.addClass(NotFoundExceptionMapper.class);
deployment.addAllDependencies();
container.deploy(deployment);
}
}
这是我的群体演示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>com.domain</groupId>
<artifactId>swarm-demo</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<version.wildfly-swarm.core>1.0.0.Beta2</version.wildfly-swarm.core>
<version.wildfly-swarm.plugin>1.0.0.Beta2</version.wildfly-swarm.plugin>
</properties>
<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs-weld</artifactId>
<version>${version.wildfly-swarm.core}</version>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>ejb</artifactId>
<version>${version.wildfly-swarm.core}</version>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jpa</artifactId>
<version>${version.wildfly-swarm.core}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.191</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<version>${version.wildfly-swarm.plugin}</version>
<configuration>
<mainClass>nl.paston.swarm.demo.Main</mainClass>
<jvmArguments>
<!-- Needs to be tuned for performance -->
<jvmArgument>-Xmx128m</jvmArgument>
</jvmArguments>
</configuration>
<!-- Needed for fat jar creation -->
<executions>
<execution>
<id>package</id>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
为了进行比较,这是Wildfly服务器应用程序的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>com.domain</groupId>
<artifactId>wildfly-demo</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<version.java-ee>7.0</version.java-ee>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${version.java-ee}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
为了完整性,这里是ExceptionMapper的代码:
@Provider
public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {
@Override
public Response toResponse(NotFoundException e) {
System.out.println("Exceptionmapper called!");
return Response.ok().build();
}
}
boundery:
@Path("/customer")
@ApplicationScoped
public class CustomerResource {
@Inject CustomerService cs;
@GET @Path("/") @Produces(MediaType.APPLICATION_JSON)
public List<Customer> get() {
return cs.getAll();
}
}
控制器:
@Stateless
public class CustomerService {
@PersistenceContext
private EntityManager em;
public List<Customer> getAll() {
return em.createNamedQuery(Customer.FIND_ALL, Customer.class).getResultList();
}
}
和实体:
@Entity @Table(name = "CUSTOMER")
@NamedQueries({@NamedQuery(name = Customer.FIND_ALL, query = "SELECT c FROM Customer c")})
@XmlRootElement
public class Customer implements Serializable {
public static final String FIND_ALL = "Customer.findAll";
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(length = 40)
private String name;
public Customer() { }
public Customer(String name) { this.name = name; }
public int getId() { return id; }
protected void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
// toString, equals and hashCode overrides omitted
}
答案 0 :(得分:1)
最新版本已解决此问题。请查看http://wildfly-swarm.io/posts/announcement-1-0-0-beta7/
请记住,自Beta6以来,我们一直在提供包含WildFly Swarm不同部分的所有必要版本的BOM,因此将BOM导入depMan将为您提供正确的版本
答案 1 :(得分:0)
我刚尝试使用最新的swarm,这似乎不是问题。也许你升级它会工作吗?
我怀疑这个问题与您的NotFoundException
有关。它的定义是什么?你在哪里扔它?