In this tutorial we will learn how to create a simple Hello World JAX-RS application for WildFly using the built-in RESTEasy API.

Pre-requisites: You need a WildFly application server. Read here how to get started with it: Getting started with WildFly

wildfly hello world jboss hello world

Next, we will create a project using a Maven archetype to bootstrap a Web application project. You can use one simple like the following one:

mvn -DarchetypeGroupId=org.codehaus.mojo.archetypes \
-DarchetypeArtifactId=webapp-javaee7 \
-DgroupId=com.mastertheboss -DartifactId=helloworld \
-Dversion=1.1 -Dpackage=com.mastertheboss \
-Darchetype.interactive=false --batch-mode --update-snapshots \
archetype:generate

Now, let's configure the project dependency file (pom.xml) so that we can include REST Endpoints in it:

<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mastertheboss</groupId>
  <artifactId>helloworld</artifactId>
  <packaging>war</packaging>
  <version>1.0.0</version>
  <name>Demo REST Service</name>
  <url>http://www.mastertheboss.com</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <version.server.bom>17.0.0.Final</version.server.bom>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.wildfly.bom</groupId>
        <artifactId>wildfly-javaee8-with-tools</artifactId>
        <version>${version.server.bom}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.jboss.spec.javax.ws.rs</groupId>
      <artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.enterprise</groupId>
      <artifactId>cdi-api</artifactId>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>

        </configuration>
      </plugin>
      <plugin>
        <groupId>org.wildfly.plugins</groupId>
        <artifactId>wildfly-maven-plugin</artifactId>
        <version>2.0.0.Final</version>
      </plugin>
    </plugins>
  </build>
</project>

Are you using Gradle to build your project? No worries, check out this tutorial: Gradle tutorial for WildFly users

Then let's build the Hello World example. We basically need three items:

A simple Model Class:

package com.mastertheboss.jaxrs.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class SimpleProperty {
	public SimpleProperty() {

	}
 
	private String key;
	private String value;

	public SimpleProperty(String key, String value) {
		super();
		this.key = key;
		this.value = value;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}

	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
}

The we need a REST Endpoint:

package com.mastertheboss.jaxrs.service;

import com.mastertheboss.jaxrs.model.SimpleProperty;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("/hello")
public class HelloService {
	@GET
	@Path("/text")
	public String getHello () 
	{
		return "hello world!";
	} 
	@GET
	@Path("/json")
	@Produces(MediaType.APPLICATION_JSON)
	public SimpleProperty getPropertyJSON ()
	{
        SimpleProperty p = new SimpleProperty("key","value");
		return p;
	}
	@GET
	@Path("/xml")
	@Produces(MediaType.APPLICATION_XML)
	public SimpleProperty getPropertyXML () 
	{
        SimpleProperty p = new SimpleProperty("key","value");
		return p;
	}
}

And finally, a REST Activator class, that will expose our services under the /rest Path:

package com.mastertheboss.jaxrs.activator;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;


@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
   
}

That's all. As we have included the Maven WildFly plugin in the project, let's build and deploy it with:

$ mvn install wildfly:deploy

Now we can test all available endpoints. The first one will just return a text String:

$ curl http://localhost:8080/helloworld/rest/hello/text
hello world!

The second endpoint produces a JSON output:

$ curl http://localhost:8080/helloworld/rest/hello/json
{"key":"key","value":"value"}

And finally, the last one produces an XML output:

$ curl http://localhost:8080/helloworld/rest/hello/xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><simpleProperty><key>key</key><value>value</value></simpleProperty>

That's all! Congratulations, you have just mastered the Hello World WildFly tutorial

Source code: https://github.com/fmarchioni/mastertheboss/tree/master/javaee/helloworld

0
0
0
s2sdefault