Giter Site home page Giter Site logo

fatjar's Introduction

FatJar

FatJar simple API to quick prototyping and portable web services. See Main::main test class for examples.

Please see the fatjar.test.Main::main class for detailed examples, also check the interfaces HttpClient, JSON and Server under fatjar package, added a sample MyEntity class under fatjar.sample package as DB operations sample

Please find the swagger.yaml file as an example for service documentation

to create and start the server

Server.create().listen(80, "localhost").start();

Listen to an http method on a path

Server.create()
                .listen(80, "localhost")
                .get("/", (req, res) -> {
                    res.setContent("Welcome");
                    res.write();
                })
                .start();

To JSON and from JSON example

Server.create()
                .listen(80, "localhost")
                .get("/toJSON", (req, res) -> {
                    res.setContent(JSON.toJson(new MyPOJO("john", 101)));
                    res.write();
                })
                .post("/fromJSON", (req, res) -> {
                    MyPOJO myPOJO = JSON.fromJson(new String(req.getBody()), MyPOJO.class);
                    res.setContent(JSON.toJson(myPOJO));
                    res.write();
                })
                .start();

To make an Http request

Server.create()
                .listen(80, "localhost")
                .get("/httpClient", (req, res) -> {
                    try {
                        String content = HttpClient.create()
                                .url("http://ip.jsontest.com/")
                                .method(HttpMethod.GET)
                                .send()
                                .getContentAsString();
                        res.setContent("got content: " + content);
                    } catch (HttpClient.HttpClientException e) {
                        res.setContent("error: " + e);
                    }
                    res.write();
                })
                .start();

Sample Test Request

After Main::main start

request :   GET   http://localhost:80/
response:   Welcome
request :   GET   http://localhost:80/Hi
response:   type "http://localhost:80/Hi?name=john" in your browser
request :   GET   http://localhost:80/Hi?name=john
response:   Hello john
request :   GET   http://localhost:80/toJSON
response:   {"age":101,"name":"john"}
request :   POST   http://localhost:80/fromJSON
            BODY   {"age":101,"name":"john"}
response:   {"age":101,"name":"john"}

A database method added, you may find the count, find, insert, update and delete methods under "/db" handler

request :   GET   http://localhost:80/db
response:   {"id":73,"name":"johnny"}

Example exception throw method

request :   GET   http://localhost:80/throwException
response:   {"error": "fatjar.Server$ServerException: tojsonexception", "request": {...}, "status": "500"}

Below example will make a request to a uri that is not handled

request :   GET   http://localhost:80/notfound
response:   {"error":"fatjar.Server$ServerException","status":"500"}

/badRequest will return html content, this content is created by the Status.STATUS_BAD_REQUEST handler, registered with the .register(Status.STATUS_BAD_REQUEST, ...) method

request :   GET   http://localhost:80/badRequest
response:   <h1>BAD REQUEST!</h1> 
request :   GET   http://localhost:80/httpClient
response:   got content: {"ip": "123.123.123.123"}

fatjar's People

Contributors

alicanalbayrak avatar canmogol avatar

Watchers

 avatar  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.