我的休息服务不起作用

时间:2016-10-01 13:31:23

标签: java rest jax-ws wildfly

我正在尝试休息,但它不起作用。

我的项目资源管理器是;

My project explorer is

我的web.xml是;

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>HelloRest</display-name>
  <servlet-mapping>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

IHello.java;

@Path("hello")
public interface IHello {
    @GET
    @Path("sayHello")
    public String sayHello(@QueryParam("name") String name);
}

Hello.java;

public class Hello implements IHello {
    @Override
    public String sayHello(String name) {
        return "Hello: " + name;
    }
}

我用浏览器调用它;

 http://localhost/HelloRest/rest/hello/sayHello?name=me

但它返回Not found。

如果我打电话;

http://localhost/HelloRest/aa/index.html,

我可以看到index.hmtl的内容。

我的问题是什么?我该如何解决?

注意:我使用Wildfly-10.1

部署它

2 个答案:

答案 0 :(得分:0)

尝试更改您的Servlet部署代码,如下所示。 通过此部署,您的Web应用程序中找到的所有@Path类都可以使用URL模式/ rest / *

<servlet>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

答案 1 :(得分:0)

<@> @ Narayana的答案完全有效,但您可以将所有web.xml放在一起,并且只有一个额外的Java文件,目前与您的代码位于同一目录中,看起来像:

package hellorest;

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

/**
 * Used to bootstrap the rest services.  This class is not directly used.
 */
@ApplicationPath("/services")
public class RestApplicationConfig extends Application {
    // intentionally empty
}

注意“/ services”注释 - 这表示您的服务将在/ services中访问,所以在您的示例中它可能是http://localhost:8080/HelloRest/services/hello/sayHello

相关问题