简体   繁体   English

我的休息服务不起作用

[英]My rest service does not work

I am trying to make a rest call but it does not work. 我试图打个电话,但它不起作用。

My project explorer is; 我的项目浏览器是;

我的项目浏览器是

My web.xml 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; IHello.java;

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

Hello.java; Hello.java;

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

I call it from browser with; 我用浏览器来称呼它;

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

but it returns Not found. 但返回“找不到”。

If I call; 如果我打电话

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

I can see the content of index.hmtl. 我可以看到index.hmtl的内容。

What is my problem and how can I fix it? 我的问题是什么,如何解决?

Note: I deploy it with Wildfly-10.1 注意:我将其与Wildfly-10.1一起部署

Try to change your Servlet deployment code as given below. 尝试按以下说明更改Servlet部署代码。 With this deployment all the @Path classes found in your web application will be available with the URL pattern /rest/* 通过此部署,您的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's answer is perfectly valid but you could instead forgo the web.xml all together and have a single additional Java file that, for now, lives in the same directory as your code and looks something like: @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
}

Notice the "/services" annotation - this says that your services will be accessed at /services so in your example it is likely to be http://localhost:8080/HelloRest/services/hello/sayHello 注意“ / services”注释-这表示将在/ services处访问您的服务,因此在您的示例中,它可能是http:// localhost:8080 / HelloRest / services / hello / sayHello

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

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