服务无法找到资源(Wildfly 10.0,JAX-RS)

时间:2016-04-08 16:05:29

标签: web-services maven jax-rs wildfly-10

我正在学习REST服务并面对问题:尽管我已经尝试了各种方法来演示它们,但RestEasy无法找到我的资源。

例外:

template <int k>
struct Myclass_helper { using type = list<typename Myclass_helper<k-1>::type>; };

template < >
struct Myclass_helper<0> { using type = int; };

template <int k>
using Myclass = typename Myclass_helper<k>::type;

其中RestfullService是我的项目的名称,/ service - applicationPath,hello - resource。此页面上未显示任何错误(404等)。

我的web.xml (顺便说一句,我在使用网络应用服务器3.0时遇到了关于web.xml内容的有争议的方法。根据官方手册,这不是必需的,但很多人争论它。最佳做法是什么?)

Failed to execute: javax.ws.rs.NotFoundException: RESTEASY003210: Could not find resource for full path: http://localhost:8080/RestfullService/services/hello

我的pom.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" version="3.0">
  <display-name>RestEasy sample Web Application</display-name>
  <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>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.store.model.WebConfig</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>resteasy-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <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>/</param-value>
    </context-param> 
</web-app>

ServiceInitialisation.java (包含和不包含此类主体的选项都没有帮助):

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>RestfullService</groupId>
    <artifactId>RestfullService</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webXml>webapp\WEB-INF\web.xml</webXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- <executions> <execution> <phase>package</phase> <configuration> <webXml>\webapp\WEB-INF\web.xml</webXml> 
                </configuration> </execution> </executions> -->
            <plugin>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <modules>
                        <ejbModule>
                            <!-- property configurations goes here -->
                        </ejbModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>JBoss repository</id>
    <url>https://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>3.0.4.Final</version>
            <scope>provided</scope>
        </dependency>
         <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>3.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-servlet-initializer</artifactId>
            <version>3.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>3.0.4.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxb-provider</artifactId>
            <version>3.0.4.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>jaxrs-api</artifactId>
            <version>3.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-multipart-provider</artifactId>
            <version>3.0.4.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
                <groupId>org.jboss.spec</groupId>
                <artifactId>jboss-javaee-7.0</artifactId>
                <version>1.0.0.Final</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.annotation</groupId>
            <artifactId>jboss-annotations-api_1.2_spec</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>2.0-EDR1</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.servlet</groupId>
            <artifactId>jboss-servlet-api_3.0_spec</artifactId>
            <version>1.0.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20140107</version>
        </dependency>
    </dependencies>
</project>

最后服务

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

@ApplicationPath("/services")
public class ServiceInitialisation extends Application{
//  private Set<Object> singletons = new HashSet<Object>();
//    private Set<Class<?>> classes = new HashSet<Class<?>>();
//
//  public ServiceInitialisation() {
//      singletons.add(new GoodsWebServiceImpl());
//  }
//
//  @Override
//  public Set<Object> getSingletons() {
//      return singletons;
//  }
//
//    @Override
//    public Set<Class<?>> getClasses() {
//        return classes;
//    }

}

我会感激任何意见,因为网上所有可用的解决方案都已经没有成功尝试过。 先谢谢

更新: 由于我的机器上只成功部署了少量手册,我想在项目创建过程中选择了不正确的设置。 我以这种方式为REST创建项目:创建动态Web项目 - &gt;将其转换为Maven - &gt;添加依赖项等 - &gt;试图部署

他们的截图: The image is here

UPDATE2 而且,我注意到每次启动WildFly时都会遇到同样的错误。我试图创建一些具有各种依赖项的项目,但结果是一样的。怎么处理呢?

import java.util.List;
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;
import com.store.entity.Item;
@Path("/")
public interface GoodsWebService {

    @GET
    @Path("/hello")
    @Produces("application/json")
    public Response greet();

     // id: \\d+ - regex for cheching if it is int  @GET    @Path("{id:
     \\d+}")    
    @Produces("application/json")   
    public Response
    getInfoById(@PathParam("id") int id);

    @GET
    @Path("/all")
    @Produces("application/json")
    public List<Item> getAllItems();
}

    package com.store.restService;

import java.util.ArrayList;
import java.util.List;

//import org.json.JSONException;
//import org.json.JSONObject;
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;

import org.json.JSONObject;

import com.store.entity.Item;
import com.store.entity.Shop;

@Path("/")
public class GoodsWebServiceImpl implements GoodsWebService
{
    private static List<Item>data;
    private static List<Shop>shops1;
    private static List<Shop>shops2;
    private static List<Shop>shops3;
    private static Shop shop1;
    private static Shop shop2;
    private static Shop shop3;
    static {
        shop1 = new Shop(1,4.55, 1);
        shop2 = new Shop(2,9.99, 2);
        shop3 = new Shop(3,6,0);
        shops1.add(shop1); shops1.add(shop2);
        shops2.add(shop3); shops2.add(shop1);
        shops3.add(shop2); shops3.add(shop3);
        data = new ArrayList<>();
        data.add(new Item(1, "hand", shops1));
        data.add(new Item(2, "pen", shops2));
        data.add(new Item(3, "ball", shops2));
    }
    public static Item getItemById(int id){
        for(Item e: data){
            if(e.getId()==id) return e;
        }
        throw new RuntimeException("Can't find Item with id -" + id);
    }
    public static List<Item> getAll(){
        return data;
    }
    @Override
    public Response greet(){
        String result = "Hello!";
        return Response.status(200).entity(result).build();
    }
    @Override
    public Response getInfoById(int id){
        JSONObject jsonObject = new JSONObject();
        Item item = getItemById(id);
        jsonObject.put("Id ", item.getId()); 
        jsonObject.put("Mpn ", item.getMpn());
        jsonObject.put("Shop ", item.getShops());

        String result = "Output:\n" + jsonObject;
        return Response.status(200).entity(result).build();
    }
    @Override
    public List<Item> getAllItems(){
//      JSONObject jsonObject = new JSONObject();
//      List<Item> items = getAll();
//      jsonObject.put("List", items);
//      JSONArray jArray = jsonObject.getJSONArray("List");
//      for(int i=0; i<jArray.length(); i++){
//          System.out.println(jArray.getJSONObject(i).getString("id"));
//          System.out.println(jArray.getJSONObject(i).getString("mpn"));
//          System.out.println(jArray.getJSONObject(i).getString("shop"));
//      }
        return getAll();
    }
}

1 个答案:

答案 0 :(得分:0)

您的POM中不需要web.xmlresteasy-servlet或任何明确的RESTEasy依赖项。

您最好先从一个简单的工作示例开始,例如: https://github.com/wildfly/quickstart/tree/10.x/jaxrs-client

尽管名称如此,但此示例不仅包括JAX-RS客户端,还包括JAX-RS服务。