繁体   English   中英

我的休息服务不起作用

[英]My rest service does not work

我试图打个电话,但它不起作用。

我的项目浏览器是;

我的项目浏览器是

我的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

但返回“找不到”。

如果我打电话

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

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

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

注意:我将其与Wildfly-10.1一起部署

尝试按以下说明更改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>

@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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM