带有Rest的Tomcat-HTTP状态500-Servlet执行引发异常 [英] Tomcat with Rest - HTTP Status 500 - Servlet execution threw an exception

查看:86
本文介绍了带有Rest的Tomcat-HTTP状态500-Servlet执行引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一些教程之后,我正在尝试编写一个非常简单的Restful Web服务.我到处搜索,但经过几次尝试都找不到适合我的问题的解决方案.我正在使用Netbeans 8.0.2和Apache Tomcat 8.0.15(与netbeans一起安装).除了Tomcat之外,我在Tomcat上遇到了几个问题.

I'm trying to code a very simple restful webservice, following some tutorials. I'm searched everywhere but I can't find a solution after several tries that would fit my problem. I'm using Netbeans 8.0.2 and Apache Tomcat 8.0.15 (installed with netbeans). I've had several issues with Tomcat but I managed to solve them, except for this one.

HelloWorld.class

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

To test my service, I followed this: https://netbeans.org/kb/docs/websvc/rest.html#test-rest

我在做什么错了?

推荐答案

您有一个jar冲突问题.查看您所有的库.也许您使用的是其他Jersey versiosn的罐子

You have a jar conflicts problem. Review all your libs. Maybe you are using some jars from different Jersey versiosn

这篇关于带有Rest的Tomcat-HTTP状态500-Servlet执行引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆