Excpetionmapper 不适用于 Wildfly Swarm,但可以在 Wildfly Server 中使用

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

我正在尝试 运行 WildFly Swarm 中的一个简单实体 - 控制器 - 边界应用程序。这工作正常,但是当我添加一个 ExceptionMapper 来捕获 NotFoundException 时,这在 WildFly 服务器 10 上有效,但在 WildFly 群中无效。这是 Shrinkwrap 中的错误吗?这是 Wildfly Swarm 中的错误吗?还是我在 Shrinkwrapping 部署中做错了什么?

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

这是我的集群演示 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>

为了比较,这是我的 pom.xml Wildfly 服务器应用程序:

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

边界:

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

我刚刚尝试了最新的 swarm,这似乎不是问题。也许你升级它会工作?

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

我怀疑这个问题与您的 NotFoundException 有关。它的定义是什么?你往哪儿扔?

最新版本已解决此问题。请看http://wildfly-swarm.io/posts/announcement-1-0-0-beta7/

请记住,自 Beta6 以来,我们一直在提供包含 WildFly Swarm 不同部分的所有必要版本的 BOM,因此将 BOM 导入 depMan 将为您提供正确的版本