【问题标题】:Simple REST API with WildFly 8WildFly 8 的简单 REST API
【发布时间】:2014-04-25 12:26:50
【问题描述】:

首先,我是这个环境的新手。我以前开发过 Java,但不是为应用程序服务器开发的。从来没有这样做过,我以前从未与 JBoss 或 WildFly 合作过。

我已经能够设置和运行 WildFly 服务器,并通过 127.0.0.1:9990 访问它。当我部署 .war 文件时,服务器没有反应,我无法访问 URL。

WildFly 服务器确实表明我的部署成功并且处于活动状态,然后我尝试访问:127.0.0.1:8080/RECAPP-API/rest/message/test 并收到 404(找不到页面错误)。

我正在使用 Maven,所以首先,我的pom.xml

<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.test.recapp.rest</groupId>
  <artifactId>RECAPP-API</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>3.0.6.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>3.0.6.Final</version>
    </dependency>
  </dependencies>
</project>

我的JSONService.java

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Path("/message")
public class JSONService {

    @GET
    @Path("/{param}")
    @Produces("application/json")
    public Response printMessage(@PathParam("param") String msg) {
        String result = "Restful example: " + msg;
        return Response.status(200).entity(result).build();
    }

}

最后,我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0">
  <display-name>RECAPP-API</display-name>

  <context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
  </context-param>

  <context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/rest</param-value>
  </context-param>

  <listener>
    <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
  </listener>

  <servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>


</web-app>

感谢您的帮助。

【问题讨论】:

  • 第一个 Rest-Application 一开始可能有点混乱。看起来您缺少 JaxRsActivator。您需要它来为应用程序创建一个有效的上下文。 @ApplicationPath("/rest") public class JaxRsActivator extends Application { /* class body intentionally left blank */ }另外你可以看看 TicketMonster 教程:jboss.org/jdf/examples/ticket-monster/tutorial/Introduction
  • 太糟糕了,教程没有提及您要解释的内容;它取决于代码生成,因此并没有真正解释多少基础。
  • @Goot - 为什么 WildFly 会让你这样做???我知道旧版本的 JBoss 你只需要配置 web.xml。在我的机器上不起作用,所以我按照你说的去做,它起作用了。

标签: java maven jboss resteasy wildfly


【解决方案1】:

快速入门的最佳方法是使用此依赖项。

<dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-api</artifactId>
  <version>7.0</version>
  <scope>provided</scope>
</dependency>

并添加一个扩展应用程序类的类

@ApplicationPath("rest")
public class ConfigApp extends Application {
   public ConfigApp(){
   }
}

就是这样。没有 web.xml 更改(仅 web.xml 不是必需的)。

并使用 host:port/&lt;warname&gt;/rest/&lt;endpoint path&gt; 访问您的休息端点

【讨论】:

    【解决方案2】:

    Wildfly Quickstart 中,他们似乎更喜欢使用 JaxRsActivator 类:添加它以配置您的 REST 服务。

    /**
     * A class extending {@link Application} and annotated with @ApplicationPath is the Java EE 6 "no XML" approach to activating
     * JAX-RS.
     *
     * <p>
     * Resources are served relative to the servlet path specified in the {@link ApplicationPath} annotation.
     * </p>
     */
    @ApplicationPath("/rest")
    public class JaxRsActivator extends Application {
        /* class body intentionally left blank */
    }
    

    正如 cmets 所说,这是一种非 xml 方法。

    【讨论】:

      【解决方案3】:

      你的例子看起来是正确的。

      这有助于重新启动您的 jboss 服务器并重新部署您的 WAR 以排除潜在的缓存。

      此外,您的web.xml 可以缩短为使用javax.ws.rs.core.Application,如下所示。

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns="http://java.sun.com/xml/ns/javaee" 
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
            id="WebApp_ID" version="3.0">
         <display-name>RECAPP-API</display-name>
      
         <servlet-mapping>
             <servlet-name>javax.ws.rs.core.Application</servlet-name>
             <url-pattern>/rest/*</url-pattern>
         </servlet-mapping>
      </web-app>
      

      【讨论】:

        猜你喜欢
        • 2016-06-26
        • 2014-12-21
        • 2014-11-30
        • 1970-01-01
        • 2017-07-28
        • 2016-12-22
        • 2015-03-18
        • 2015-05-30
        • 2014-04-10
        相关资源
        最近更新 更多