Giter Site home page Giter Site logo

tiny-service's Introduction

TinyService

====

Golang风格的Java微型API框架

  • 支持Servlet规范或使用内嵌Jetty server
  • 用于构建简单REST API
  • 借鉴了SparkJava , 相比原来精简了代码并省去了个人来讲不必要的JSP支持 , 并添加了Java 7支持

Getting started

public class TestLaunch {
    public static void main(String args[]) {

        Route.get("/",  Route.get("/", (request, response) -> "poi~" );

        Tiny.server("localhost", 8000);

    }
}

Then view at: http://localhost:8080

Examples

  Route.get("/users/:name", (request, response) ->"name: " + request.params(":name"));
  Route.get("/redirect", (request, response) -> {
            response.redirect("/somework");
            return null;
        });
 Route.get("/protected", (request, response) -> {
            Action.halt(403, "I don't think so!!!");
            return null;
        });

WebServer

  • launch with jetty
Tiny.server("localhost", 8080);

or

Tiny.server("localhost", 8080, 1000, 20, 2000);
  • launch with servlet

->web.xml

   <filter>
        <filter-name>TinyFilter</filter-name>
        <filter-class>cn.shiroblue.core.TinyFilter</filter-class>
        <init-param>
            <param-name>applicationClass</param-name>
            <param-value>"Your APP Class"</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>TinyFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

->an implementation of TinyApplication

public class TestApp implements TinyApplication {
    @Override
    public void init() {
         Route.get("/",  Route.get("/", (request, response) -> "poi~" );
    }
    @Override
    public void destroy() {
    }
}

Render

  • return Json
	RenderFactory.setDefaultRender(new JsonRender());
  • use Gson
     RenderFactory.setDefaultRender(new Render() {
                private Gson gson = new Gson();
                @Override
                public String rend(Object o) throws Exception {
                    return gson.toJson(o);
                }
            });

tiny-service's People

Contributors

whiteblue avatar

Watchers

 avatar  avatar

Forkers

xiaodin1 super-l

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.