Skip to content

payneteasy/api-servlet

Repository files navigation

Github Actions Status CircleCI Quality Gate Status

Simple API Servlet for JSON

Features

  • supported: jackson, gson

Setup with dependency managers

Maven

<repositories>
    <repository>
        <id>pne</id>
        <name>payneteasy repo</name>
        <url>https://maven.pne.io</url>
    </repository>
</repositories>

<dependency>
  <groupId>com.payneteasy</groupId>
  <artifactId>api-servlet</artifactId>
  <version>1.0-5</version>
</dependency>

Gradle

compile 'com.payneteasy:api-servlet:1.0-5'

How to use

Create a service class

public class HelloServiceSample {

    public ResponseMessageSample sayHello(RequestMessageSample aName) {
        ResponseMessageSample response = new ResponseMessageSample();
        response.text = "Hello " + aName.name;
        return response;
    }
}

Create servlet mapping with Jackson

    Server                jetty   = new Server(8080);
    ServletContextHandler context = new ServletContextHandler(jetty, "/api", ServletContextHandler.NO_SESSIONS);
    ObjectMapper          mapper  = new ObjectMapper();
    HelloServiceSample service    = new HelloServiceSample();
    
    context.addServlet(new ServletHolder(new JacksonApiServlet<>(service::sayHello, RequestMessageSample.class, ResponseMessageSample.class, mapper)), "/user/*");

Create with Gson

    Server                  jetty   = new Server(8080);
    ServletContextHandler   context = new ServletContextHandler(jetty, "/api", ServletContextHandler.NO_SESSIONS);
    Gson                    gson    = new GsonBuilder().setPrettyPrinting().create();
    GsonJettyContextHandler handler = new GsonJettyContextHandler(context, gson);

    HelloServiceSample      service = new HelloServiceSample();

    handler.addApi("/user/*", service::sayHello, RequestMessageSample.class);

License

The ApiServlet library is licensed under the Apache License 2.0