Giter Club home page Giter Club logo

boostraped-jersey-restapi's Introduction

GsonJerseyProviderExample

GsonJerseyBodyParser

GsonJerseyProvider is an example on how to use the Google Gson Library as an Provider on Jersey JAX-RS applications.

The original Version of the GsonJerseyProvidor.java can be found here: @eclipsesource:

Creating modern applications often involves consuming REST based web services. One of the more popular ways to publish a REST service in Java is the JAX-RS (Jersey) specification. It allows you to very easily enhance your REST resources with Java annotations.

by Moritz Post.

I needed an working example, because I had some issues getting started only with his class and I couldn't just leave it dead on my disk.

The configuration part

First to get the class of Moritz Post running we need to do a little bit of configuration so Jersey knows to load the provider (web.xml):

<servlet>
	<servlet-name>Services</servlet-name>
	<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
	<init-param>
		<param-name>jersey.config.server.provider.packages</param-name>
		<param-value>
  			com.dominikangerer.gson.provider.v1.util,
  			com.dominikangerer.gson.provider.v1.controller
  		</param-value>
	</init-param>
	<init-param>
		<param-name>jersey.config.server.provider.scanning.recursive</param-name>
		<param-value>false</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>

Now you should be able to run the example from Moritz Post but you will have to add the GsonUtil to get the version I provided in this repository as well - so let me explain it a little bit to you.

GsonUtil

In this GsonUtil I added the DateFormat yyyy-MM-dd'T'HH:mm:ss.SSSXX as default DateFormat to Gson by adding new registerTypeAdapter for serializing and deserializing. I also added the possibility to load a preconfigurated instance of Gson to show only properties with @Expose Annotation.

Container

Why? Javascipt Frameworks like Ember can be used with an RESTAdapter which provide such an pattern of responses - so why not return such an formated Json?

In this Repository you can find an simple Controller for GETting an List<Resource> and simply GET an Resource by id. To Achieve this on an structured way I added an new container package which can be used for resources and also for an single resource. It also capsules the whole Response.status(Response.Status.X).entity(Y).build(); stuff for you so you can simply use it like this in your Controller to return the action you performed with the right success status:

return ResourceContainer.ok();
return ResourceContainer.ok(Resource);
return ResourceContainer.ok(Resource List);
return ResourceContainer.ok(Resource ArrayList);
return ResourceContainer.ok(Resource Collection);
return ResourceContainer.updated();
return ResourceContainer.updated(Resource);
return ResourceContainer.created(Resource);
return ResourceContainer.deleted();

Version

1.0.1

Installation

You need an Container like Tomcat and clone this repository:

git clone [email protected]:DominikAngerer/java-GsonJerseyProvider.git

Import it into your favourite IDE (i'm using Eclipse Luna) and enjoy.

Calls

Don't mind if you get an 404 on path: /. Now it will return: Okay, I hated the 404 Page here ;), because I really did.

  • Get List of Resources http://localhost:8080/gson.provider/v1/resources

  • get Resource by Id http://localhost:8080/gson.provider/v1/resources/b1e581bf-8265-480c-a460-50c860df15be

Links

Stackoverflow

boostraped-jersey-restapi's People

Contributors

reftel avatar sargue avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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