Giter Site home page Giter Site logo

javalin-api-library's Introduction

javalin-api-library

A simple to use library for creating APIs using Javalin

How to implement?

Maven:

coming soon

Information: The maven project already builds the following dependencies:

- org.slf4j.slf4j-simple [1.7.28]
- io.javalin.javalin [3.7.0]
- com.google.code.gson.gson [2.8.6]

You don't have to implement them in your project.

Registering endpoints

public class Example {

    public static void main(String[] args){
        // 1.): Start a new Javalin instance
        Javalin app = Javalin.create().start(7700);

        // 2.): Register a new RateLimiter instance (optional)
        long allowedRequestsPerMinute = 60;
        RateLimiter rateLimiter = new RateLimiter(allowedRequestsPerMinute, ctx -> ctx.status(HttpStatus.TOO_MANY_REQUESTS_429));
        
        // 3.): Register a new HttpServer
        HttpServer server = new HttpServer(app, rateLimiter);

        // 4.): Register the '/test' endpoint
        server.endpoint("/test", HandlerType.GET, new TestEndpoint());
    }

}

class TestEndpoint extends Endpoint {

    @Override
    public void handle(Context ctx) {
        ctx.status(HttpStatus.OK_200).result("Hello, world!");
    }

}

Using the response builder

public class Example {

    public static void main(String[] args){
        // 1.): Start a new Javalin instance
        Javalin app = Javalin.create().start(7700);

        // 2.): Register a new RateLimiter instance (optional)
        long allowedRequestsPerMinute = 60;
        RateLimiter rateLimiter = new RateLimiter(allowedRequestsPerMinute, ctx -> ctx.status(HttpStatus.TOO_MANY_REQUESTS_429));
        
        // 3.): Register a new HttpServer
        HttpServer server = new HttpServer(app, rateLimiter);

        // 4.): Register the '/test' endpoint
        server.endpoint("/test", HandlerType.GET, new TestEndpoint());
    }

}

class TestEndpoint extends Endpoint {

    @Override
    public void handle(Context ctx) {
        String response = new ResponseBuilder(HttpStatus.OK_200)
            .withResponseType(ResponseType.SUCCESS)
            .addData("message", "Hello, world!")
            .toJson();
        ctx.status(HttpStatus.OK_200).result(response);
    }

}

The response will look like this:

{
  "status": 200,
  "type": "success",
  "data": {
    "message": "Hello, world!"
  }
}

javalin-api-library's People

Contributors

cerus avatar lus avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.