Monday 9 March 2015

Creating Web Services and a Rest Server with JAX-RS and Jetty

Creating a WebService in Java is remarkably easy. To add it to a ServletContainer and deploy it to an embedded WebServer is only a few more lines of code. 

Let's create a simple calculator with a couple of functions as an example of a WebService. The calculator will compute the squareRoot and square of any number.  It will return a simple JSON response with the name of the action, the input and the output.

Before we start this is the Gradle configuration you will need:


This is the code for the Calculator:

The annotations determine the type of REST action to be applied to the method @GET, @PUT etc. The @Path annotation determines the URI of the request and the @Produces annotation determines how the response will be returned.  In our case we choose JSON, the conversion of which is all handled seamlessly.

In order to deploy our WebService we need a ServletContainer for which we will use Jersey and a WebServer into which we can drop the container for which we will use Jetty.

This is the code for the RestServer:



Once you have run the RestServer you will be able to test it with this URL.


http://localhost:8080/calculator/squareRoot?input=16


A really nice way to run queries from IntelliJ is to use the inbuilt REST Client which can be found under the tools menu.


  
When you run the REST Client you will get this Response:



It's a really easy way to test a RESTful server.

21 comments:

  1. Good article, but should it be JAX-RS instead of JAX-RX?
    Im just comment, because I ended up here looking for a rest server api with Observables, and because your title mentions JAX-RX, google brought me to your article.

    ReplyDelete
    Replies
    1. I know it's been a bit of a delay :) but you are correct.

      Delete
  2. thank you for your help,
    i can connect to licalhost:8080/... and recieve the JSOn data but
    when i try to connect to that link with websocket a gave an error: "Didn't switch protocols, expected status <101>, but got <200>"

    can u please help me to solve this. it is about 1 week that i couldn't solve that

    ReplyDelete
  3. How can I implement SSL for this server. Actually I am trying to implement REST Service server over HTTPS.

    ReplyDelete
  4. Apple is already working on iOS 12 beta build for iPhone and iPad users. Developers around the world are soon going to test the latest iOS update, but general public is still wondering when iOS 12 release is going to happen for them!

    ReplyDelete
  5. The RAID cluster comprises of in excess of one hard drives installed as a solitary unit for high limit and speed than the single hard drive with an outer reinforcement. Check Out FixingBlog

    ReplyDelete
  6. In regards to your brand, think of your website as the face of your podcast. This is what listeners will see on a daily basis that relates to your podcast here

    ReplyDelete
  7. It is the database planners employment to guarantee that the database structure and information lexicon are improved to help the information stockpiling recovery and execution objectives of the undertaking. https://www.dbdesigner.net/

    ReplyDelete
  8. Online Pharmacy Pills is one of the trusted Online Pharmacy from where you can purchase your medicines at a cheaper price.In our daily life we have to tackle with various types of pain.Also it is always not possible to visit a doctor and consult it with them as we human being are so much busy with our life that we often get time to take care of our health.Pain sometimes becomes so unbearable that we are bound to take painkillers.Pain gives a very unpleasant feeling to our body.Although various types of painkillers are available in the market but among of those Soma pills are the best.Soma pills gives you immediate relief from any type of pain and also it have very mild side effect.
    [url=https://www.onlinepharmacypills.net/product/soma-350-mg/]Soma 350mg online[/url]

    ReplyDelete
  9. Bad idea indeed! These software programs are exceedingly complex. They work in complex steps and write to the disk which can result in an overwrite of the original data. Select one wrong option and you can bid goodbye to your data! data recovery centre

    ReplyDelete
  10. While there are countless dialects, huge numbers of them have a great deal of similitudes; this implies once you learn one dialect great, as a rule you will probably get another one far faster. https://www.techpally.com/interactive-programming-learning-websites/

    ReplyDelete
  11. The circle platters are finicky about what comes into contact with them and will respond antagonistically to residue, fingerprints and some other flotsam and jetsam. data recovery centre

    ReplyDelete
  12. Thanks for an interesting blog. What else may I get that sort of info written in such a perfect approach? I have an undertaking that I am just now operating on, and I have been on the lookout for such info. audio transcription services uk

    ReplyDelete
  13. The Deal With Databases As an example of the type of information that you are going to have to be staying on top of,dbdesigner.net let's take a moment and talk about databases.

    ReplyDelete
  14. Around 44% of all data misfortune is brought about by equipment disappointment. It's imperative to ensure that you promptly shut your framework down on the off chance that you speculate that hard drive has slammed. recover deleted data

    ReplyDelete
  15. very good article.thank you for sharing these content. Ever since our inception in 2009, we have been on an epic ride and paved our way to becoming the Best IT Company In Surat.

    ReplyDelete

  16. Very Nice Post

    All festival wallpaper
    www.allfestivalwallpaper.com

    Gujju Fun
    www.gujjufun.com

    Global surat
    www.globalsurat.com

    ReplyDelete
  17. Very Nice Post

    All festival wallpaper
    www.allfestivalwallpaper.com

    Gujju Fun
    www.gujjufun.com

    Global surat
    www.globalsurat.com

    Torrent Counetr Movies
    www.torrentcountermovies.com

    Torrent Counetr Movies
    www.torrentcountermovies.info

    ReplyDelete
  18. Router Tech Support Phone Number +1-844-456-4180.
    Check Out FixingBlog
    as a company was the first in its field to launch a wireless router to share a single internet connection with multiple devices.

    ReplyDelete
  19. Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share. business setup in dubai

    ReplyDelete
  20. I can set up my new idea from this post. It gives in depth information. Thanks for this valuable information for all,.. business portfolio case

    ReplyDelete