Tomcat with Rest - HTTP Status 500 - Servlet execu

2019-08-23 18:27发布

问题:

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

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>

The error

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

What am I doing wrong?

回答1:

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