RESTful Webservices on Google App Engine

≡放荡痞女 提交于 2019-11-30 10:07:48

I started one year ago to develop an app with Jersey and Google App Engine. Great experience from my side, but I have never worked with Restlet ..

I try here to summarize the main difficulties I found in GAE integration:

  • Jersey version: 1.6 works
  • I suggest you to use Jackson (version 1.7.1) for json representation

web.xml fragment:

<servlet>
<servlet-name>jersey</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>***package-with-your-classes***;org.codehaus.jackson.jaxrs</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Configurator:

@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private AnnoxAnnotationReader annotationReader;
private JAXBContext context;
private Class<?>[] classTypes = new Class[] { .. all your classes .. };

public JAXBContextResolver() {
annotationReader = new AnnoxAnnotationReader();
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBRIContext.ANNOTATION_READER, annotationReader);      
try {
    this.context = JAXBContext.newInstance(classTypes, properties);
} catch (JAXBException e) {
    ..  
}
public JAXBContext getContext(Class<?> objectType) {
    return context;
}

.. as you can see I use Annox to avoid annotations inside my model classes!

Hope it helps! Michele Orsi

I have tried Restlet and was not satisfied with it: it tries to do to much and is not JAX-RS at it's core (they have it as an add-on). I had problems make it work in various settings (request would not be routed to the method, but when only changing method order it would start working. WTF?!). Also their documentation is scarce and inconsistent.

I took a look at Jersey: there were some problems with running on GAE at that time (resolved via help on support forum). Also I found their docs to not be that good.

Finally, I went with Resteasy/Jackson: docs are superb, works with Maven out of the box, full control over config, security and error handling (exceptions thrown in code returned as JSON error object). Basically no issues. You can look at an example here: LeanEngine REST classes.

Also, if used with JSON/Jackson (make sure to force Jackson 1.9, as built in 1.7 is old) you get a lot of control over how your classes are mapped to JSON: one-to-one, wrapping/embedding, adapter-pattern, etc..

I've been using Restlet on GAE for about 6 months. I chose it in part because they also have editions for Android and GWT, which are also part of my product mix, and I thought it would be simplest to go with the same thing everywhere.

In contrast to Peter K's comment, I found the documentation to be pretty good. In addition to the online documentation at restlet.org, there is a 400-page ebook (Restlet in Action) available from Manning that goes quite in-depth. Possibly the ebook came out subsequent to Peter's evaluation.

That being said, it's a pretty big library with a lot of features, which is a double-edged sword. One the one hand, every time I want to solve a new kind of problem, it seems like Restlet already has something built-in to make it easier. On the other hand, I find it to be challenging to debug through the Restlet source when I'm trying to figure out a problem -- all that flexibility and functionality adds up to a broad and deep class hierarchy, and it's hard sometimes to see how the pieces fit together. If you're building a substantial app, I think it's worth a look, because I don't think you'll run into many limitations with Restlet. However, I haven't used RestEasy, so I can't make an informed comparison to it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!