https://github.com/payneteasy/api-servlet
Universal Json servlet to create Rest API
https://github.com/payneteasy/api-servlet
Last synced: 4 months ago
JSON representation
Universal Json servlet to create Rest API
- Host: GitHub
- URL: https://github.com/payneteasy/api-servlet
- Owner: payneteasy
- License: apache-2.0
- Created: 2019-05-12T00:17:37.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2025-05-10T13:41:26.000Z (about 1 year ago)
- Last Synced: 2025-07-20T10:41:19.312Z (about 1 year ago)
- Language: Java
- Homepage:
- Size: 168 KB
- Stars: 0
- Watchers: 4
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://github.com/payneteasy/api-servlet/actions)
[](https://circleci.com/gh/payneteasy/api-servlet)
[](https://sonarcloud.io/dashboard?id=com.payneteasy%3Aapi-servlet)
Simple API Servlet for JSON
==========================
## Features
* supported: jackson, gson
## Setup with dependency managers
### Maven
```xml
pne
payneteasy repo
https://maven.pne.io
com.payneteasy
api-servlet
1.0-5
```
### Gradle
```groovy
compile 'com.payneteasy:api-servlet:1.0-5'
```
How to use
------------
Create a service class
```java
public class HelloServiceSample {
public ResponseMessageSample sayHello(RequestMessageSample aName) {
ResponseMessageSample response = new ResponseMessageSample();
response.text = "Hello " + aName.name;
return response;
}
}
```
### Create servlet mapping with Jackson
```java
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
```java
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