Tag Archives: rest

Was playing around a bit with AngularJS. Seems like a great way to structure your templates at client vs server-side. After playing around with Jersey and bunch of other Java REST frameworks, I finally decided to go with just a basic Servlet container and have that provide some JSON data over a REST style interface. Just too much badly document weird requirements there in the frameworks, couldn’t really figure out the benefits.. anyway

Getting the data to AngularJS is not hard. Just have the Servlet return the page with the template. Then for dynamically querying data from server in Angular I can make a call such as

    function myController($scope,$http) {
                success(function(response) {
                    $scope.values = response.my_data;

Using response.variablename I can also put the contents of the returned JSON object up for parsing by other parts of the template, or just use plain “response” for using it as is. But this only covers the simple scenario of having no parameters in the query. What if I need to perform a post with some request parameters?

I tried

    function myController($scope,$http) {
        var data = "msg="+JSON.stringify({count:10});
        $http.post("http://localhost:5555/hello", data).
                success(function(response) {
                    $scope.values = response;

I thought this would allow me to access it in a Servlet using request.getParameter(“msg”). But this just returns null.. Seems to be because the post from AngularJS is not a HTML Form post, so the Servlet container does not parse it as I was expecting. So we need to write our own parser..

  private void showPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Read from request
    StringBuilder buffer = new StringBuilder();
    BufferedReader reader = req.getReader();
    String line;
    while ((line = reader.readLine()) != null) {
    String data = buffer.toString();

    log.debug("Latest request:"+data);

    try {
      JSONObject json = new JSONObject(data);

And then we have the request data in the “json” variable. And it is better now to forget the “msg=..” part from above attempt, and just send over the plain JSON.

Looking at the REST frameworks, I guess the simplest way would be to just embed the parameters in the request URL such as /hello/my_parameter. Would probably need a ServletFilter to parse all that.. Maybe someday..

So I am also back to hacking to…

So I am also back to hacking to get a REST service up, with some more excuses. Anyway, once again tried Restlet as I like its minimal set of jar files and simple interface. But even if it sometimes (from my googling) seems to be advertised as well documented, it actually seems rather poorly documented. Going to the Jersey user guide and reading that really helps me understand how Jersey works. There seems to be nothing similar in Restlet, it is more spread around with few lines about all the different platforms and extensions with just little info but not quite enough on anything. JBoss RestEasy on the other hand is a huge bunch of jar files for every little task one in itself. So Jersey it is then, which seems to be nicely documented but only a “reasonable set of jars”…

So how do I get Jersey to run inside OSGI?

Just using it in the standard way expressed in the Jersey docs, results in the exception

com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.

I guess this is due to the standard OSGI classloader issues, as also noted for example in


But how do I fix it? Writing my own ResourceConfig class seems like a huge task for something this simple, and something that could easily break in future Jersey versions. So this is the code I used to get the exception

Dictionary<String, String> config = new Hashtable<String, String>();
config.put("com.sun.jersey.config.property.packages", "my.package.rest");
httpService.registerServlet("/rest", new ServletContainer(), config, null);


So now my Resource classes are in package “my.package.rest”. But it all fails, doesn’t it.

Luckily the Jersey user guide has a nice example of using Jersey inside OSGI. The trick seems to be to not use the “packages” property but rather to change the above to

Dictionary<String, String> config = new Hashtable<String, String>();
config.put("javax.ws.rs.Application", JerseyApplication.class.getName());
httpService.registerServlet("/rest", new ServletContainer(), config, null);


So the difference is using the Application class to explicitly list the Resource classes. I guess this results in using the correct classloader, probably the one that loads the Application as well. Well, at least it works 🙂


edit: the info for using Jersey with a servlet container is perhaps best given in the JavaDoc for package com.sun.jersey.spi.container.servlet, for example, at the time of this writing available at (browse your Jersey javadoc package if not):
for OSGI the jersey user guide OSGI example code (download from the link in user guide) is the best ..

rest hard

with all the hype on REST architecture, I thought it would be nice. Well, after some time learning what it is about really, I think it still has potential to be good. But some experiences to remind myself:

tried restlet first, which seems nice. has one single jar file to deploy, includes client and server, and a simple webserver. so nothing else is needed. whee. well, if i only wanted to run restlet against reslet, sure. but try to link that to anything else and i could not find documentation on how to do it. what type of request data does it expect in different cases? etc. etc. maybe now i could be a bit smarter but anyway.. the docs reall suck imo.

then off to jersey, the sun (ok, oracle now) reference implementation. plenty of jar files, a complete mess to deploy. but documents itself nice at least to the level of defining how to expect the @get @post etc. to behave. including the data types of “text/plain” etc. so i can actualy imagine connecting to it from another system with other tools. well, try running that in osgi.. it should come with examples but the whole things is bloody hard to get. finally, tried to get the whole sources and compile it with maven. oh yeah.. after huge downloads by maven it keeps failing. and btw maven proxy configurations suck completely. did i mention i hate maven and all the stupid dependencies and sick directory structures it brings on me? lets put a single class in a separate project and make 100 of those.. uu yeah. and just running “mvn” fails with weird errors. I should do “mvn install” which leads to dependency download hell. end result, unable to run inside osgi due to grizzly server weird classloader issues.

in general, i have to say i hate these lets be smart frameworks that do it al for you through reflection tricks. always leads to problems. just give me simple configuration api please. lots of issues passing any objects to different instances (that is, not doable). at least xmlrpc was hackable.

well, in the end i ended up doing REST in OSGI through the felix httpservice. the whole concept is simple enough to just make a servlet to handle the get, post and other requests. biggest problem is that with the OSGI httpservice you cannot configure a servlet mapping to create urls like restlet and jersey support where a path in the url leads to a resource. well, with the support for filters and their patterns, this is doable it seems and the whole servlet-mapping is not needed. whee, so it works not by tossing all the fancy frameworks. as usual..