【问题标题】:Tomcat with Rest - HTTP Status 500 - Servlet execution threw an exception带有 Rest 的 Tomcat - HTTP 状态 500 - Servlet 执行引发异常
【发布时间】:2015-07-27 10:13:24
【问题描述】:

我正在尝试编写一个非常简单的宁静网络服务,遵循一些教程。我到处搜索,但经过几次尝试后我找不到适合我的问题的解决方案。我正在使用 Netbeans 8.0.2 和 Apache Tomcat 8.0.15(与 netbeans 一起安装)。我在使用 Tomcat 时遇到了几个问题,但我设法解决了这些问题,除了这个。

HelloWorld.class

package com.example;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
//Path: http://localhost/<appln-folder-name>/hello
@Path("/hello")
public class HelloWorld {

    // HTTP Get Method
    @GET

    // Path: http://localhost/<appln-folder-name>/hello/world
    @Path("/world")

    // Produces JSON as response
    @Produces(MediaType.APPLICATION_JSON) 
    public String doHello(){

        JSONObject obj = new JSONObject();
        try {
            obj.put("hello", "world");

        } catch (JSONException e) {
            // TODO Auto-generated catch block
        }

        System.out.println(obj.toString());

        return obj.toString();

    }

}

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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>restful_example</display-name> <!-->project name<-->
  <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>com.example</param-value> <!-->package name<-->
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

错误

为了测试我的服务,我遵循了这个:https://netbeans.org/kb/docs/websvc/rest.html#test-rest

我做错了什么?

【问题讨论】:

  • 如果您使用的是泽西岛,您能分享一下您使用的是哪个版本的泽西岛?我怀疑 Jersey 和 JAX-RS 的错误版本组合或 Jersey 的多个版本,因此错误的版本被选中。
  • 我使用的是 Jersey 1.19 和 JAX-RS 2.0(对于 Jersey,我从官方网站下载并复制了 jars,但对于 JAX-RS,我直接通过 netbeans 添加)。

标签: java rest tomcat netbeans


【解决方案1】:

你有一个 jar 冲突问题。查看所有库。也许您正在使用来自不同 Jersey 版本的一些罐子

【讨论】:

  • 如果我删除 JAX-RS 2.0 库,我会在测试时得到一个空白页。如果我删除 Jersey 库,我会得到 HTTP 状态 404。我该怎么办?编辑:当我删除 Jersey 库时,Apache 日志中也会出现错误
  • 我发现了问题所在。这是库和我测试的方式。 This way 没有显示任何内容,我必须在浏览器中编写路径(在我的情况下为 localhost:8084/restful_example/hello/world)。我现在的问题是为什么我在编写一个 restful web 服务时不能使用 JAX-RS 2.0。
猜你喜欢
  • 2015-09-26
  • 2014-10-26
  • 2015-08-23
  • 2012-11-18
  • 1970-01-01
  • 2013-01-25
  • 2018-06-29
  • 2017-11-28
  • 1970-01-01
相关资源
最近更新 更多