Wildfly-swarm:无法访问 jax-rs 资源(404 未找到)
Wildfly-swarm: cannot access jax-rs resource (404 Not found)
我想试试 wildfly-swarm。我用 Jax-rs 部分和一个简单的 hello world 资源创建了一个项目。我 运行 它但我得到 404 未找到。
这是我的 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>org.aca.studies</groupId>
<artifactId>swarm</artifactId>
<version>1.0</version>
<properties>
<version.wildfly-swarm>2017.7.0</version.wildfly-swarm>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<version>${version.wildfly-swarm}</version>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
<execution>
<id>start</id>
</execution>
<execution>
<id>stop</id>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
<version>${version.wildfly-swarm}</version>
</dependency>
</dependencies>
这是我的 Jax-rs 资源
package org.aca.studies;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/app")
public class Resource {
@GET
@Path("/greet")
@Produces("text/html")
public String greet() {
return System.currentTimeMillis() + "";
}
}
如您所见,它非常简单。也许我错过了什么。我没有添加带注释的 @ApplicationPath class 因为根据文档 (https://wildfly-swarm.gitbooks.io/wildfly-swarm-users-guide/content/common/jax-rs.html) this fraction adds one by default. Another thing I picked up from the examples on GitHub (https://github.com/wildfly-swarm/wildfly-swarm-examples/tree/master/jaxrs/jaxrs) 是这样的:
Since WildFly Swarm apps tend to support one deployment per executable, it automatically adds a jboss-web.xml to the deployment if it doesn't already exist. This is used to bind the deployment to the root of the web-server, instead of using the .war's own name as the application context.
所以这就是我尝试从 http://localhost:8080/app/greet
访问我的资源的原因
根据你的 pom,我认为问题是你没有 <packaging>war</packaging>
,这意味着创建了一个 JAR。
如果你把 Maven 打包改成 WAR 那么应该没问题
我想试试 wildfly-swarm。我用 Jax-rs 部分和一个简单的 hello world 资源创建了一个项目。我 运行 它但我得到 404 未找到。
这是我的 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>org.aca.studies</groupId>
<artifactId>swarm</artifactId>
<version>1.0</version>
<properties>
<version.wildfly-swarm>2017.7.0</version.wildfly-swarm>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<version>${version.wildfly-swarm}</version>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
<execution>
<id>start</id>
</execution>
<execution>
<id>stop</id>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
<version>${version.wildfly-swarm}</version>
</dependency>
</dependencies>
这是我的 Jax-rs 资源
package org.aca.studies;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/app")
public class Resource {
@GET
@Path("/greet")
@Produces("text/html")
public String greet() {
return System.currentTimeMillis() + "";
}
}
如您所见,它非常简单。也许我错过了什么。我没有添加带注释的 @ApplicationPath class 因为根据文档 (https://wildfly-swarm.gitbooks.io/wildfly-swarm-users-guide/content/common/jax-rs.html) this fraction adds one by default. Another thing I picked up from the examples on GitHub (https://github.com/wildfly-swarm/wildfly-swarm-examples/tree/master/jaxrs/jaxrs) 是这样的:
Since WildFly Swarm apps tend to support one deployment per executable, it automatically adds a jboss-web.xml to the deployment if it doesn't already exist. This is used to bind the deployment to the root of the web-server, instead of using the .war's own name as the application context.
所以这就是我尝试从 http://localhost:8080/app/greet
访问我的资源的原因根据你的 pom,我认为问题是你没有 <packaging>war</packaging>
,这意味着创建了一个 JAR。
如果你把 Maven 打包改成 WAR 那么应该没问题