Simple API Servlet for JSON
Features
- supported: jackson, gson
Setup with dependency managers
Maven
<dependency>
<groupId>com.payneteasy</groupId>
<artifactId>api-servlet</artifactId>
<version>1.0-3</version>
</dependency>
Gradle
compile 'com.payneteasy:api-servlet:1.0-3'
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