java - Jersey : the resource is not available

标签 java web-services jersey

我尝试将一个简单的 helloworld 作为 jersey 项目执行,但 Web 服务根本没有启动。而且我不知道问题出在哪里。

我的主要类(class)如下:

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

 @Path("/hello")
 public class HelloWorld {
 @GET
 @Produces(MediaType.TEXT_PLAIN)
 public String getMessage() {
    return "Hello world!";
 }
 }

这是我的 web.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">

  <display-name>Restful Web Application</display-name>
  <servlet>
    <servlet-name>jersey-XMLExample-serlvet</servlet-name>
    <servlet-class>
        com.sun.jersey.spi.container.servlet.ServletContainer
    </servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>jmu.zak</param-value>
    </init-param>

    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
    <servlet-name>jersey-XMLExample-serlvet</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
    </web-app>

和我的index.jsp

   <html>
   <head>
   <title>MOXy JAX-RS JSON Provider Example</title>
   <style type="text/css"><!--
   .figure {
   text-align: center;
   margin: 20px
    }
   .cliSample {
   background-color: lightgray
   }
   --></style></head>

  <body>
  <p>A <a href="http://wadl.java.net/#spec">WADL description</a> may be  accessed at the URL:</p>
  <blockquote>
  <code>
  <a  href="http://localhost:8080/webapi/application.wadl">http://localhost:8080/webapi/application.wadl</a>
  </code></blockquote>

   <p>The resource is available at</p>
   <blockquote>
   <code>
   <a href="http://localhost:8080/webapi/hello">http://localhost:8080/webapi/hello</a>
   </code>
  </blockquote>
  </body>
  </html>

正如您所看到的,这个例子非常非常简单。这就是为什么我对我的网络服务从未启动感到如此沮丧......

预先感谢您,如果您帮我解决这个问题,您将挽救我的生命

最佳答案

您错过了添加应用程序子类

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

@ApplicationPath("api")
public class RestApplication extends Application{

}

通过此示例,您可以从 http://localhost:8080/webapi/api/hello 检索资源

关于java - Jersey : the resource is not available,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36157157/

相关文章:

java - 生成 900 万个唯一的随机数字字符串

java - jackson :反序列化为每个值都具有正确类型的 Map<String, Object>

c# - SOAP web 服务时出现 InvalidOperationException - 但在开发机器上工作

java - 从数据库中检索其余数据( Jersey )

java - 带有 MySQL 数据库的 spring-data-elasticsearch

java - 访问正在运行的小程序的内部

wcf - Windows Azure 上的 Web 服务?

web-services - 如何测试来自 JMeter 的 @DELETE restful 服务调用

用于 Tomcat 的 REST Web 服务

java - 使用 proguard 进行混淆后出现 Jersey 错误,尝试实例化抽象类