GithubHelp home page GithubHelp logo

restfull-web-services-app's Introduction

Example of RESTful web services with JAX-RS

This is a little explanation a REST architecture and we will build a simple RESTfull application with JAX-RS library called Jersey. Representational State Transfer or REST is a simple way to organize interactions between independent system. It can be used wherever HTTP can. One of the idea of the REST that application should have specific purposes, like retrieving, adding, deleting or updating data(HTTP service request). And actions should be performed no more than one at a time.

  • POST - create a new resource(resource is contained in the body of the POST request)
  • GET - retrieves a resource from the server
  • PUT - updates the state of a known resource
  • DELETE - deletes a resource on the server

Here’re some HTTP response codes which are often used with REST.

  • 200 ok - indicates that request was successful
  • 201 created - request was successful and a resource was created
  • 400 bad request - request was malformed
  • 401 unauthorized - must perform authentication before accessing the resource
  • 404 not found - resource could not be found
  • 500 internal server error

First, create a standard web project from maven archetype: mvn archetype:generate -DgroupId=com.dmitrynikol.rest -DartifactId=RESTfulApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false Now run a command to add dependencies in project, eclipse can resolve them, we just convert maven based Java project into to support Eclipse IDE. mvn eclipse:eclipse -Dwtpversion=2.0 After that you will see two new files “.classpath” and “.project”. And now you can easily import a project into your Eclipse workspace. So, now we have a maven web stub project.

  • Here is the link to the Maven pom.xml file with all dependencies that we need.
  • POJO class User object that mapping to JSON via JAXB with @XmlRootElement annotation.

Time to create UserService class, that represent a Jersey resources as an POJO and will be manipulated by different HTTP methods. Service class contains many annotations to create different behaviours. And Jersey provides a set of Java annotations that can be used to define the web service structure.

  • @Path - configure the URL pattern the class will handle
  • @Post, @GET, @PUT and @DELETE - method will answer to the HTTP POST, GET, PUT and DELETE request
  • @Produces - specifies the MIME type of the response that is returned to the client
  • @Consumes - specifies the content type that the service will accept as input
  • @QueryParam - mark a field that will be extracted from the URL in a GET request
  • @DefaultValue - mark the value that will be used by default
  • @PathParam - mark a field that will be extracted from a field in the URL path

And don’t forget to add com.sun.jersey.api.json.POJOMappingFeature to web.xml file that integrate JSON with Jersey. It will make Jersey support JSON/object mapping.

That’s all. Now we’re ready to start the rest service with tomcat instance via command - mvn tomcat:run

restfull-web-services-app's People

Contributors

dmitrynikol avatar

Watchers

James Cloos 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.