2016-03-05 60 views
0

我想在WildFly Swarm中运行一个简单的实体 - 控制器 - boundery应用程序。这工作正常,但是当我添加一个ExceptionMapper来捕获NotFoundException这可以在WildFly服务器10上工作,但不在WildFly群集中。这是Shrinkwrap中的错误吗?这是Wildfly Swarm中的一个bug吗?或者我在收缩包装部署方面做错了什么?Excpetionmapper不适用于Wildfly Swarm,但在蜻蜓服务器中工作

下面是该群主类的特定代码:

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> 

为了完整,这里是代码fo R上的ExceptionMapper:

@Provider 
public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> { 

    @Override 
    public Response toResponse(NotFoundException e) { 
     System.out.println("Exceptionmapper called!"); 
     return Response.ok().build(); 
    } 
} 

的boundery:

控制器:

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

回答

1

这已通过最新版本解决。请看看http://wildfly-swarm.io/posts/announcement-1-0-0-beta7/

记住,因为Beta6我们已经提供了一个BOM与WildFly群的不同部分的所有必要的版本,所以在导入BOM到depMan会给你正确的版本

+0

谢谢肯!你真棒! –

0

我只是想与最新的群,它似乎不是一个问题。也许你升级它会工作?

https://github.com/johnament/wildfly-swarm-examples/blob/master/jaxrs/jaxrs-shrinkwrap/src/main/java/org/wildfly/swarm/examples/jaxrs/shrinkwrap/Main.java

我怀疑这个问题与您NotFoundException做。它的定义是什么?你在哪扔?

+0

你最近的群体是什么意思?我正在使用1.0.0.Beta2,它是最新的发行版本。我也试过1.0.0.CR1-SNAPSHOT。同样的问题。 –

+0

要清楚。我的问题与'NotFoundException'完全相同。当你尝试请求一个不存在的url时,让我们说http:// localhost:8080/customer/foo你看到文本“Exceptionmapper called!”在日志中?或者它仍然抛出NotFoundException? –

+0

啊,现在我明白了。它总是有用的,以确保包括进口,以避免含糊不清。无论如何,我刚刚向群组报告了一个错误。 https://issues.jboss.org/browse/SWARM-327 –