Giter Site home page Giter Site logo

gitter-badger / katharsis-servlet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zachncst/katharsis-servlet

0.0 1.0 0.0 187 KB

Generic Servlet Adapter of Katharsis JSON:API library

Home Page: http://katharsis.io/

License: Apache License 2.0

Java 100.00%

katharsis-servlet's Introduction

katharsis-servlet

Join the chat at https://gitter.im/katharsis-project/katharsis-servlet Build Status Coverage Status Stories in Ready Maven Central

Generic Servlet Adapter of Katharsis JSON:API middleware library.

Introduction

This module aims to provide a generic invoker module for Katharsis JSON:API middleware library (https://github.com/katharsis-project/katharsis-core). This module can be used in simple servlet or filter, servlet-based application framework such as Spring Framework, or even non-ServletAPI-based frameworks such as Portal/Portlet, Wicket, etc.

How to use this in my Servlet Filter

This module provides an abstract class, AbstractKatharsisFilter.java. Basically you need to override the following method at least as well:

    abstract protected KatharsisInvokerBuilder createKatharsisInvokerBuilder();

Also see SampleKatharsisFilter.java as a servlet filter implementation example, and web.xml as a configuration example.

How to use this in my Servlet

This module provides an abstract class, AbstractKatharsisServlet.java. Basically you need to override the following method at least:

    abstract protected KatharsisInvokerBuilder createKatharsisInvokerBuilder();

Also see SampleKatharsisServlet.java as a servlet implementation example, and web.xml as a configuration example.

How to integrate with my IoC (Dependency Injection) Container

You can override #createKatharsisInvokerBuilder() method in either SampleKatharsisFilter.java or SampleKatharsisServlet.java to use your own JsonServiceLocator component.

For example, you can create a new JsonServiceLocator to get a bean (singleton or prototype) from Spring Web Application Context like the following example:

    /**
     * NOTE: A class extending this must provide a platform specific {@link JsonServiceLocator}
     *       instead of the (testing-purpose) {@link SampleJsonServiceLocator} below
     *       in order to provide advanced dependency injections for the ResourceRepository beans.
     */
    @Override
    protected KatharsisInvokerBuilder createKatharsisInvokerBuilder() {
        return new KatharsisInvokerBuilder()
                .resourceSearchPackage(getResourceSearchPackage())
                .resourceDefaultDomain(getResourceDefaultDomain())
                .jsonServiceLocator(new JsonServiceLocator() {
                    @Override
                    public <T> T getInstance(Class<T> clazz) {
                        // assuming the following in this example:
                        // - you can get the BeanFactory through WebApplicationContextUtils and servlet context.
                        // - your ResourceRepository beans are retrieved by the type through the BeanFactory.
                        BeanFactory beanFactory = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
                        return beanFactory.getBean(clazz);
                    }
                });
    }

In the example above, a custom JsonServiceLocator was provided to find ResourceRepository beans by the type from the underlying WebApplicationContext.

For demonstration purpose, the example assumes that it can retrieve Repository beans from the WebApplicationContext in a Web MVC application. So you will need to register the JSON API Repository beans by either annotation-based or XML configuration based approach in the WebApplicationContext.

You can also choose a more generic approach by using a bean which implements BeanFactoryAware instead of using WebApplicationContextUtils.

In the spring-boot-simple-example, it demonstrates how to register the filter bean itself by using @Configuration-annotated class (see WebConfig.java) and how to let the filter bean (SpringBootSampleKatharsisFilter.java) get access to the BeanFactory by implementing BeanFactoryAware, for instance. So, the filter bean can retrieve ResourceRepository beans by invoking on BeanFactory#getBean(Class) directly.

Please read more about The IoC container if you need more detail about Spring IoC container and beans configuration.

You have probably got the idea on how to integrate with other containers as well from the example.

How to Add this module in my Project

Add the following dependency:

    <dependency>
      <groupId>io.katharsis</groupId>
      <artifactId>katharsis-servlet</artifactId>
      <version>${katharsis-servlet.version}</version>
    </dependency>

See changes.xml for details.

Demo in a Web Application

Please run the following command in this project root folder:

    $ mvn -Prun clean verify

Visit http://localhost:8080/katharsis/ and test out each JSON API link.

katharsis-servlet's People

Contributors

cedricziel avatar meshuga avatar woonsan avatar woonsanko avatar

Watchers

 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.