GAE Restful Web Service in Java

2019-09-01 02:23发布

问题:

I want to develop restful web service in java for google app engine. the tools i am using are

Eclipse Kepler Service Release 2 (Build id: 20140224-0627)
Google AppEngine Java SDK 1.9.2

i have used many versions of Jersey, e.g. 1.8, 1.5 etc. if i run the project on local server it works fine but when i deploy it on the google app engine i can able to open the welcome web page but can't able to access web service.

as mentioned in https://code.google.com/p/googleappengine/wiki/WillItPlayInJava link, GAE support Jersey 1.5 but when i import those jars files in my project i can't able to deploy it on GAE.

i don't know which version of Jersey should i use with Google AppEngine Java SDK 1.9.2 or should i use another version of SDK.

my web.xml file

<?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" version="2.5">

    <display-name>com.cit.cittracker</display-name>
    <servlet>
        <servlet-name>Sample Web 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.cit.cittracker</param-value>
        </init-param>
        <init-param> 
            <param-name>com.sun.jersey.config.feature.DisableWADL</param-name> 
            <param-value>true</param-value> 
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Sample Web Service</servlet-name>
        <url-pattern>/restservices/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

my webservice class

package com.aakib.sample;

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

@Path("/hello")
public class SampleHello {

    /*This method is called if TEXT_PLAIN is requested*/
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
        return "Hello Jersey" + "(In Text Format)";
    }

    /*This method is called if XML is requested*/
    @GET  
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello>Hello Jersey</hello>"
                                            + "<format>XML</format>";  
    }

    /*This method is called if HTML is requested*/
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
        return "<html> " + "<title>" + "Hello Jersey" + "</title>"
            + "<body><h1>" + "Hello Jersey" + "</h1><h2>In HTML Format<h2></body>" + "</html> ";
    }

    /*This method is called if HTML is requested and have two parameters*/
    @GET
    @Path("{name}")
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHelloWith1Param(@PathParam("name") String vName) {
        return "<html> " + "<title>" + "Hello Jersey" + "</title>"
            + "<body><h1>Hello " + vName + "</h1><h2>In HTML Format</h2>" + 
                "<h3>1 Value Passed</h3>" + "</body>" + "</html> ";
    }

    /*This method is called if HTML is requested and have two parameters*/
    @GET
    @Path("{name}/{surname}")
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHelloWith2Param(@PathParam("name") String vName, @PathParam("surname") String vSurname) {
        return "<html> " + "<title>" + "Hello Jersey" + "</title>"
                + "<body><h1>Hello " + vName + " " + vSurname + "</h1><h2>In HTML Format</h2>" + 
                    "<h3>2 Value Passed</h3>" + "</body>" + "</html> ";
    }
}

when i tried with Jersey 2.5 & Jersey 1.17 i got the error on local server as

Apr 12, 2014 2:10:45 AM java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.
Apr 12, 2014 2:10:47 AM com.google.apphosting.utils.config.AppEngineWebXmlReader readAppEngineWebXml
INFO: Successfully processed B:\Programming\Java EE\CIT Tracker Web Service\war\WEB-INF/appengine-web.xml
Apr 12, 2014 2:10:47 AM com.google.apphosting.utils.config.AbstractConfigXmlReader readConfigXml
INFO: Successfully processed B:\Programming\Java EE\CIT Tracker Web Service\war\WEB-INF/web.xml
Apr 12, 2014 2:10:47 AM com.google.appengine.tools.development.SystemPropertiesManager setSystemProperties
INFO: Overwriting system property key 'java.util.logging.config.file', value 'C:\Program Files (x86)\Google AppEngine Java SDK 1.9.2\config\sdk\logging.properties' with value 'WEB-INF/logging.properties' from 'B:\Programming\Java EE\CIT Tracker Web Service\war\WEB-INF\appengine-web.xml'
Apr 12, 2014 2:10:47 AM com.google.apphosting.utils.jetty.JettyLogger info
INFO: Logging to JettyLogger(null) via com.google.apphosting.utils.jetty.JettyLogger
Apr 12, 2014 2:10:47 AM com.google.apphosting.utils.jetty.JettyLogger info
INFO: jetty-6.1.x
Apr 12, 2014 2:10:48 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
  com.cit.cittracker
Apr 12, 2014 2:10:48 AM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: failed CIT Tracker WebService: java.lang.IncompatibleClassChangeError: Implementing class
Apr 12, 2014 2:10:48 AM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: failed com.google.appengine.tools.development.DevAppEngineWebAppContext@7adb5354{/,B:\Programming\Java EE\CIT Tracker Web Service\war}: java.lang.IncompatibleClassChangeError: Implementing class
Apr 12, 2014 2:10:48 AM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: failed JettyContainerService$ApiProxyHandler@4c73cedb: java.lang.IncompatibleClassChangeError: Implementing class
Apr 12, 2014 2:10:48 AM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: Error starting handlers
java.lang.IncompatibleClassChangeError: Implementing class
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at com.google.appengine.tools.development.IsolatedAppClassLoader.loadClass(IsolatedAppClassLoader.java:216)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at com.sun.jersey.api.core.ScanningResourceConfig.init(ScanningResourceConfig.java:79)
    at com.sun.jersey.api.core.PackagesResourceConfig.init(PackagesResourceConfig.java:104)
    at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:78)
    at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:89)
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:696)
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:674)
    at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:203)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557)
    at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:440)
    at org.mortbay.jetty.servlet.ServletHolder.doStart(ServletHolder.java:263)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:685)
    at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
    at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
    at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.Server.doStart(Server.java:224)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at com.google.appengine.tools.development.JettyContainerService.startContainer(JettyContainerService.java:254)
    at com.google.appengine.tools.development.AbstractContainerService.startup(AbstractContainerService.java:305)
    at com.google.appengine.tools.development.AutomaticInstanceHolder.startUp(AutomaticInstanceHolder.java:26)
    at com.google.appengine.tools.development.AbstractModule.startup(AbstractModule.java:79)
    at com.google.appengine.tools.development.Modules.startup(Modules.java:97)
    at com.google.appengine.tools.development.DevAppServerImpl.doStart(DevAppServerImpl.java:255)
    at com.google.appengine.tools.development.DevAppServerImpl.access$000(DevAppServerImpl.java:47)
    at com.google.appengine.tools.development.DevAppServerImpl$1.run(DevAppServerImpl.java:213)
    at com.google.appengine.tools.development.DevAppServerImpl$1.run(DevAppServerImpl.java:211)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.google.appengine.tools.development.DevAppServerImpl.start(DevAppServerImpl.java:211)
    at com.google.appengine.tools.development.DevAppServerMain$StartAction.apply(DevAppServerMain.java:277)
    at com.google.appengine.tools.util.Parser$ParseResult.applyArgs(Parser.java:48)
    at com.google.appengine.tools.development.DevAppServerMain.run(DevAppServerMain.java:219)
    at com.google.appengine.tools.development.DevAppServerMain.main(DevAppServerMain.java:210)

Apr 12, 2014 2:10:48 AM com.google.apphosting.utils.jetty.JettyLogger info
INFO: Started SelectChannelConnector@127.0.0.1:8888
Apr 12, 2014 2:10:48 AM com.google.appengine.tools.development.AbstractModule startup
INFO: Module instance default is running at http://localhost:8888/
Apr 12, 2014 2:10:48 AM com.google.appengine.tools.development.AbstractModule startup
INFO: The admin console is running at http://localhost:8888/_ah/admin
Apr 12, 2014 2:10:48 AM com.google.appengine.tools.development.DevAppServerImpl doStart
INFO: Dev App Server is now running

and when i try to open in browser it says

HTTP ERROR: 404

Problem accessing /. Reason:

    NOT_FOUND
Powered by Jetty://

回答1:

I used Jersey 1.18 with Google App Engine SDK 1.8.8 without problems. Try to use these versions end everything will work fine.



回答2:

The error message

java.lang.IncompatibleClassChangeError: Implementing class

shoud be the clue - though it isn't too helpful, it perhaps implies that somewhere on your classpath the application has managed to load a class and its interface but they are incompatible.

A little higher up in the error log:

WARNING: failed com.google.appengine.tools.development.DevAppEngineWebAppContext@7adb5354{/,B:\Programming\Java EE\CIT Tracker Web Service\war}: java.lang.IncompatibleClassChangeError: Implementing class

looks like it is using a path on a local windows drive, with a space in it - don't use spaces in your paths, Microsoft allowed it from Windows95 and it's caused problems ever since, and don't specify a path to your local drive on deployment - it needs to be application-relative.

Hope this helps, sorry to not be more specific.