【问题标题】:How to call rest with jax rs如何用 jax rs 调用休息
【发布时间】:2014-06-19 07:57:37
【问题描述】:

我是 jax rs 网络服务的新手。我正在从这个链接学习-http://www.vogella.com/tutorials/REST/article.html

现在,当我尝试进行第一次休息服务时,我遇到了一些错误。 这是我的服务代码

package de.vogella.jersey.first;

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 Hello {
// This method is called if TEXT_PLAIN is request
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey";
  }
 } 

我的 web.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>de.vogella.jersey.first</display-name>
<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>
<servlet>
<servlet-name>Jersey REST Service</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>de.vogella.jersey.first</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

现在,当我运行此代码时,我遇到了 HTTP 状态 404 错误。请任何人帮助我。我把http://jersey.java.net/的所有罐子都给了,请帮帮我。

【问题讨论】:

  • 你检查日志了吗...如果应用部署成功...应用上下文路径是否正确?
  • 404 is resource not found.您是否仔细检查了端点,您在 Web 服务上执行的操作?
  • 如果一切都正确...路径应该类似于 127.0.0.1://rest/hello

标签: java web-services rest jax-rs jersey-2.0


【解决方案1】:

如果你使用 jersey 2.x 你的 Web.xml servlet 应该如下

    <servlet>
      <servlet-name>Jersey Rest Service</servlet-name> 
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
      <init-param>
        <param-name>jersey.config.server.provider.packages</param-name> 
        <param-value>de.vogella.jersey.first</param-value>
      </init-param>
    </servlet>

在 jersey 2.x 中,您需要引用“org.glassfish”,您的资源 URL 将是 localhost:your_port/your_app_name/rest/hello

【讨论】:

    猜你喜欢
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2018-12-17
    • 2023-01-27
    • 2016-06-30
    相关资源
    最近更新 更多