Creating an ATOMPub server in Java that can return JSON (Using Apache Abdera)

DZone 's Guide to

Creating an ATOMPub server in Java that can return JSON (Using Apache Abdera)

· Java Zone ·
Free Resource

A few days ago I showed you how you can build an ATOM client that can convert ATOM XML to JSON. Today I’ll show you how to take Apache Abdera and create an ATOMPub server that can return ATOM XML as well as JSON. For the client part (to demo) I’m going to use the Firefox plug-in Poster.

Take a look at my previous ATOMPub server article since that will be the basis of this new server. In order to add support to return JSON there are a few steps:

1. Make sure Maven is setup to add the Apache Abdera JSON Extension
2. Modify my previous article’s code to return JSON if requested by the client

Going with the original ATOMPub server code you only need to add two lines of code to add in the JSON support:

The import for the Apache Abdera JSON Extension:

import org.apache.abdera.ext.json.JSONFilter;

Add support for the JSONFilter:

provider.addFilter(new JSONFilter());

The final code looks like this:

 * Original code created by the Apache Abdera team
 * http://abdera.apache.org/
package com.giantflyingsaucer.atompubserver;
import org.apache.abdera.protocol.server.Provider;
import org.apache.abdera.protocol.server.impl.DefaultProvider;
import org.apache.abdera.protocol.server.impl.SimpleWorkspaceInfo;
import org.apache.abdera.protocol.server.servlet.AbderaServlet;
import org.apache.abdera.ext.json.JSONFilter;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class App {
    public static void main(String... args) throws Exception {
        int port = 9002;
        try {
            port = args.length > 0 ? Integer.parseInt(args[0]) : 9002;
        } catch (Exception e) {
        Server server = new Server(port);
        ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
        ServletHolder servletHolder = new ServletHolder(new EmployeeProviderServlet());
        context.addServlet(servletHolder, "/*");
    public static final class EmployeeProviderServlet extends AbderaServlet {
        protected Provider createProvider() {
            EmployeeCollectionAdapter ca = new EmployeeCollectionAdapter();
            SimpleWorkspaceInfo wi = new SimpleWorkspaceInfo();
            wi.setTitle("Employee Directory Workspace");
            DefaultProvider provider = new DefaultProvider("/");
            provider.init(getAbdera(), null);
            provider.addFilter(new JSONFilter());
            return provider;
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            super.service(request, response);

For Maven, make sure you have this dependency (and the keep the others I already have in the prior project):


Using Poster I can populate the Abdera server with some data:

<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom">
  <title type="text">This is the title</title>
  <content type="text">Hello World</content>

Insert the data with Poster via an HTTP POST action:

Get the data back using Poster via an HTTP GET action and a querystring argument of: format=json


  "title":"Acme Employee Database",
    "name":"Acme Industries"
    "title":"Hello World",
    "content":"Hello World",
      "name":"Acme Industries"

Of course you can take my ATOM Client project and modify that to work with this ATOMPub server rather than have to use Poster.



From http://www.giantflyingsaucer.com/blog/?p=3092


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}