JAX-RS Hello world example With Wildfly

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hi, we are going to start a new tutorial series where we will learn about JAX-RS. It stands for REpresentational State Transfer. In this article we will create a JAX-RS Hello World example with Wildfly.

 

JAX-RS Hello World Example With Wildfly:

 

Adding Wildfly In Eclipse:

We are going to use the JAX-RS implementation provided by Wildfly for our JAX-RS Hello World example with wildfly. To have all required libraries in our classpath, we need to have wildfly configured in eclipse.

Open Eclipse workspace and click on Servers tab where you will see an option to add server

click to add new serverWhen the dialog box opens, select wildfly 9.x. and choose the location where it is extracted.

add wildfly 9

Creating JAX-RS Hello World example with WIldfly:

Now let us create a project in eclipse from File > New > Dynamic Web Project and name it is JAX-RS_examples. You will see the project structure as below.project structureNow we need to add JAX-RS Compatibility to it. So Right click on JAX-RS_examples and click properties, then click Project Facets. Here you need to select jax-rs. and make sure the Runtime is selected as Wildfly 9WildFly 9.x runtime

Now we are done with project setup for jax-rs hello world example with wildfly. Let us try to write some code for web service.

  1. Create a package called com.thejavageek.jaxrs
  2. Create a class ExampleApplication
  3. Create a class HelloWorldService.

Code for respective class is as below

ExampleApplication.java

Points to note

  • @ApplicationPath  annotation defines the context of web services.
  • @ApplicationPath("/rest")  means that all web services will have a context root as “/rest”

HelloWorldService.java

points to note here

  • @Path("HelloWorld")  applied at class level means that the URI to invoke the methods of this class should have “/HelloWorld” as suffix.
  • @GET annotation on sayHello() method denotes that when a HTTP GET method is invoked, then sayHello()  method should be called.
  • @Path("/sayHello")  means that when HTTP GET method is called with “/sayHello” then sayHello()  method should be executed.

Now we are done with jax-rs hello world example with wildfly. Simply deploy the application in server and start it. After it has started successfully, hit the url http://localhost:8080/JAX-RS_examples/rest/HelloWorld/sayHello and you should be able to see below page.

 

final output

I hope this article helped to create a jax-rs hello world example with wildfly

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

12 thoughts on “JAX-RS Hello world example With Wildfly

  • April 9, 2016 at 1:58 pm
    Permalink

    Very nice tutorial for basic JEE application – thank you!

    Reply
  • April 23, 2016 at 11:57 pm
    Permalink

    Hi!

    No I’m doing wrong but displays:

    Not Found.

    you can set the configuration of the web.xml or project

    Reply
    • February 12, 2017 at 6:16 pm
      Permalink

      when creating Dynamic Web Project, on third screen, make sure “Generate web.xml deployment descriptor” is checked..

      Reply
  • July 29, 2016 at 11:28 pm
    Permalink

    Hi people!
    If you’ve still this problem, You can do this:
    1. be sure that you coded exactly as in the tuto(with the classes’ name,paths)
    2.Delete your project from wildfly
    3.reimport it(You’ll have this link : “http://localhost:8080/JAX-RS_exemples/”)
    4.Add /rest/HelloWorld/sayHello.

    it’ll work.

    Reply
  • November 8, 2016 at 5:54 am
    Permalink

    It is great, but I am getting the following errors
    Is there the requirement to have .net 4.5 for WildFly10?

    PLATFORM VERSION INFO
    Windows : 6.1.7601.65536 (Win32NT)
    Common Language Runtime : 4.0.30319.18444
    System.Deployment.dll : 4.0.30319.34280 built by: FX452RTMGDR
    clr.dll : 4.0.30319.18444 built by: FX451RTMGDR
    dfdll.dll : 4.0.30319.34280 built by: FX452RTMGDR
    dfshim.dll : 4.0.41209.0 (Main.041209-0000)

    SOURCES
    Deployment url : http://localhost:8080/JAX-RS_examples/rest/HelloWorld/sayHello

    ERROR SUMMARY
    Below is a summary of the errors, details of these errors are listed later in the log.
    * Activation of http://localhost:8080/JAX-RS_examples/rest/HelloWorld/sayHello resulted in exception. Following failure messages were detected:
    + Exception reading manifest from http://localhost:8080/JAX-RS_examples/rest/HelloWorld/sayHello: the manifest may not be valid or the file could not be opened.
    + Parsing and DOM creation of the manifest resulted in error. Following parsing errors were noticed:
    -HRESULT: 0x8007001f
    Start line: 0
    Start column: 0
    Host file:
    + A device attached to the system is not functioning. (Exception from HRESULT: 0x8007001F)

    COMPONENT STORE TRANSACTION FAILURE SUMMARY
    No transaction error was detected.

    WARNINGS
    There were no warnings during this operation.

    OPERATION PROGRESS STATUS
    * [11/7/2016 5:40:19 PM] : Activation of http://localhost:8080/JAX-RS_examples/rest/HelloWorld/sayHello has started.

    ERROR DETAILS
    Following errors were detected during this operation.
    * [11/7/2016 5:40:25 PM] System.Deployment.Application.InvalidDeploymentException (ManifestParse)
    – Exception reading manifest from http://localhost:8080/JAX-RS_examples/rest/HelloWorld/sayHello: the manifest may not be valid or the file could not be opened.
    – Source: System.Deployment
    – Stack trace:
    at System.Deployment.Application.ManifestReader.FromDocument(String localPath, ManifestType manifestType, Uri sourceUri)
    at System.Deployment.Application.DownloadManager.DownloadDeploymentManifestDirectBypass(SubscriptionStore subStore, Uri& sourceUri, TempFile& tempFile, SubscriptionState& subState, IDownloadNotification notification, DownloadOptions options, ServerInformation& serverInformation)
    at System.Deployment.Application.DownloadManager.DownloadDeploymentManifestBypass(SubscriptionStore subStore, Uri& sourceUri, TempFile& tempFile, SubscriptionState& subState, IDownloadNotification notification, DownloadOptions options)
    at System.Deployment.Application.ApplicationActivator.PerformDeploymentActivation(Uri activationUri, Boolean isShortcut, String textualSubId, String deploymentProviderUrlFromExtension, BrowserSettings browserSettings, String& errorPageUrl)
    at System.Deployment.Application.ApplicationActivator.ActivateDeploymentWorker(Object state)
    — Inner Exception —
    System.Deployment.Application.InvalidDeploymentException (ManifestParse)
    – Parsing and DOM creation of the manifest resulted in error. Following parsing errors were noticed:
    -HRESULT: 0x8007001f
    Start line: 0
    Start column: 0
    Host file:
    – Source: System.Deployment
    – Stack trace:
    at System.Deployment.Application.Manifest.AssemblyManifest.LoadCMSFromStream(Stream stream)
    at System.Deployment.Application.Manifest.AssemblyManifest..ctor(FileStream fileStream)
    at System.Deployment.Application.ManifestReader.FromDocument(String localPath, ManifestType manifestType, Uri sourceUri)
    — Inner Exception —
    System.Runtime.InteropServices.COMException
    – A device attached to the system is not functioning. (Exception from HRESULT: 0x8007001F)
    – Source: System.Deployment
    – Stack trace:
    at System.Deployment.Internal.Isolation.IsolationInterop.CreateCMSFromXml(Byte[] buffer, UInt32 bufferSize, IManifestParseErrorCallback Callback, Guid& riid)
    at System.Deployment.Application.Manifest.AssemblyManifest.LoadCMSFromStream(Stream stream)

    COMPONENT STORE TRANSACTION DETAILS
    No transaction information is available.

    Reply
    • November 8, 2016 at 8:31 am
      Permalink

      I don’t think .net 4.5 is required. Have you followed all the steps exactly as shown in tutorial? I am sure whatever I have posted works properly.

      Reply
  • April 24, 2017 at 6:19 pm
    Permalink

    I do not need to generate ny web descriptor – the tutorial finely works as is
    Maybe the error depends just on having generated a web descriptor

    [my stack is: WFly 8.2.1.Final; Developer Studio 8, Jax-RS 2.0, JDK 1.7, WAR 3.1, EAR 7]

    Reply
  • May 12, 2017 at 8:33 pm
    Permalink

    hi,
    nice article. thanks
    i have a doubt while reading many blogs related to jax-rs i came across that we need to add a listener in web.xml for jax-rs. but ur example doesn’t mention it. can you tell me why we need that listener or servlet mapping in web.xml?

    Reply
    • May 15, 2017 at 9:56 am
      Permalink

      could you please provide me the link to such an article? It could be because of old java EE version where annotations were not used and xml configuration was needed.

      Reply
  • May 19, 2017 at 12:21 am
    Permalink

    Hi Prasad Kharkar,

    this tutorial is great for beginers in Wildfly, thank you!

    Reply
  • Pingback:Deployed on WildFly but still getting 404 – program faq

Leave a Reply

Your email address will not be published. Required fields are marked *