部署Wildfly服务但未找到资源

时间:2016-05-10 19:16:59

标签: java maven wildfly resteasy

我刚刚在Wildfly开始了一个webapp项目,使用maven重新开始。从日志中可以看出服务已正确部署,但我不知道我无法访问资源有什么问题。

我尝试过使用自动扫描功能,定义@Application等,但都没有。

我发布的最终代码的服务器日志的结果是:

20:57:25,781 INFO  [stdout] (ServerService Thread Pool -- 66) ************ Constructor **********

20:57:25,781 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 66) RESTEASY002225: Deploying javax.ws.rs.core.Application: class es.domain.prj.rest.JaxRsActivator
20:57:25,781 INFO  [stdout] (ServerService Thread Pool -- 66) ************ Initializing **********

20:58:03,634 INFO  [stdout] (ServerService Thread Pool -- 66) *** REST SERVICE ****

20:58:09,600 INFO  [stdout] (ServerService Thread Pool -- 66) ************ Initializing **********

20:58:11,113 INFO  [stdout] (ServerService Thread Pool -- 66) *** REST SERVICE****

20:58:13,440 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 66) RESTEASY002220: Adding singleton resource es.domain.prj.rest.RestService from Application class es.domain.prj.rest.JaxRsActivator
20:58:13,633 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 66) WFLYUT0021: Registered web context: /prj

我试图访问:

http://localhost:8080/prj/jlanza/resource - >找不到

http://localhost:8080/prj/jlanza - >找不到

http://localhost:8080/prj/resource - >找不到

你可以给我一些提示吗?资源在哪里注册?我知道它必须是一个非常愚蠢的事情,但我无法识别它。

在下面找到源代码,pom.xml和web.xml文件。

提前致谢。

的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" 
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  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>MyFirstProject</display-name>

  <!-- Auto scan REST service -->
  <context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>false</param-value>
  </context-param>

  <context-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>es.domain.prj.rest.JaxRsActivator</param-value>
  </context-param>

  <!-- this needs to be the same as servlet-mapping url-pattern -->
  <!-- set this if you map the Resteasy servlet to something other than /* -->
  <!-- 
  <context-param>
      <param-name>resteasy.servlet.mapping.prefix</param-name>
      <param-value>/resteasy</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-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

</web-app>

的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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>es.domain</groupId>
  <artifactId>prj</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <slf4j.version>1.7.21</slf4j.version>
    <!-- current log4j 2 release -->
    <log4j.version>2.5</log4j.version> 
    <jena.version>3.0.1</jena.version>
    <jboss.resteasy.version>3.0.16.Final</jboss.resteasy.version>
    <javax.cdi-api.version>1.2</javax.cdi-api.version>
    <javax.servlet.version>3.0-alpha-1</javax.servlet.version>

    <maven.plugin.version>3.5.1</maven.plugin.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- Binding for Log4J -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${log4j.version}</version>
        <exclusions>
          <exclusion>
            <artifactId>slf4j-api</artifactId>
            <groupId>org.slf4j</groupId>
          </exclusion>
        </exclusions>
    </dependency>
    <!-- Log4j API and Core implementation required for binding -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>${log4j.version}</version>
    </dependency>

    <!-- Import the JAX-RS API, we use provided scope as the API is included 
        in JBoss WildFly -->
    <dependency>
      <groupId>org.jboss.resteasy</groupId>
      <artifactId>resteasy-jaxrs</artifactId>
      <version>${jboss.resteasy.version}</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.enterprise</groupId>
      <artifactId>cdi-api</artifactId>
      <version>${javax.cdi-api.version}</version>
    </dependency>

<!-- 
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>${javax.servlet.version}</version>
      <scope>provided</scope>
    </dependency>
-->     
    <!-- Jena library -->
    <dependency>
       <groupId>org.apache.jena</groupId>
       <artifactId>apache-jena-libs</artifactId>
       <type>pom</type>
       <version>${jena.version}</version>
       <exclusions>
        <exclusion>
          <artifactId>log4j</artifactId>
          <groupId>log4j</groupId>
        </exclusion>
        <exclusion>
          <artifactId>slf4j-log4j12</artifactId>
          <groupId>org.slf4j</groupId>
        </exclusion>
       </exclusions>
    </dependency> 
  </dependencies>

  <build>
    <finalName>prj</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven.plugin.version}</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
     </plugins>
   </build>
</project>

JaxRsActivator.java

package es.domain.prj.rest;

import java.util.HashSet;
import java.util.Set;

//import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

//@ApplicationPath("/api")
public class JaxRsActivator extends Application {

    public JaxRsActivator() {
      System.out.println("************ Constructor **********");
    }

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
//        classes.add(MyResource.class);
        return classes;
    }

    @Override
    public Set<Object> getSingletons() {
  System.out.println("************ Initializing **********");
        Set<Object> singletons = new HashSet<Object>();
        singletons.add(new RestService());
        return singletons;
    }
}

RestService.java

package es.domain.prj.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

@Path("/jlanza")
public class RestService {

    public RestService() {
      System.out.println("*** REST SERVICE ****");
    }

    @GET
    @Path("resource")
    @Produces(MediaType.TEXT_PLAIN)
    public Response getResource() {
      System.out.println("***Calling function****");
      return Response.status(Status.OK).entity("working").build();
    }
}

3 个答案:

答案 0 :(得分:1)

您的应用应该可以访问:

http://localhost:8080/prj/jlanza/resource

尝试通过检查端口8080来测试您的野生动物。然后尝试使用Firefox中的一些扩展程序(如Resteasy)测试您的应用程序。您将获得可以帮助您找到问题的http响应。

答案 1 :(得分:0)

问题在于<servlet-name> <servlet><servlet-mapping>下我使用了两个不同的名字。这导致无法链接如何启动服务。

除了我收录在我的pom文件中:

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-servlet-initializer</artifactId>
        <version>${jboss.resteasy.version}</version>
    </dependency>

答案 2 :(得分:0)

我不知道这是否会帮助某人,但在我的情况下,问题是服务器没有在同步时更新。 问题的原因是服务器目标运行时的更改。

这是与此主题相关的内容:https://developer.jboss.org/thread/276899

解决方案是删除并重新创建运行时和服务器。