Search code examples
wildflyshrinkwrapwildfly-swarm

Excpetionmapper does not work with Wildfly Swarm but does work in Wildfly Server


I am trying to run a simple entity - controller - boundery application in WildFly Swarm. This works fine, however when I add an ExceptionMapper to catch the NotFoundException this does work on WildFly server 10 but not in WildFly swarm. Is this a bug in Shrinkwrap? Is this a bug in Wildfly Swarm? Or am I doing something wrong in Shrinkwrapping the deployment?

Here's the specific code for the Swarm Main class:

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);
    }
}

and here is my swarm demo 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>

For comparison, this is my pom.xml for the Wildfly server application:

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

For completeness, here is the code for the ExceptionMapper:

@Provider
public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {

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

The boundery:

@Path("/customer")
@ApplicationScoped
public class CustomerResource {

    @Inject CustomerService cs;

    @GET @Path("/") @Produces(MediaType.APPLICATION_JSON)
    public List<Customer> get() {
        return cs.getAll();
    }
}

The controller:

@Stateless
public class CustomerService {

    @PersistenceContext
    private EntityManager em;

    public List<Customer> getAll() {
        return em.createNamedQuery(Customer.FIND_ALL, Customer.class).getResultList();
    }
}

and the entity:

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

Solution

  • This has been resolved with the latest release. Please take a look at http://wildfly-swarm.io/posts/announcement-1-0-0-beta7/

    Bear in mind that since Beta6 we've been providing a BOM with all the necessary versions of the different parts of WildFly Swarm, so importing the BOM into depMan will give you the right versions