Java EE 8 & Wildfly 17 & RestEasy setup

Introduction

Java SE/EE for quite a long time was slow. Not in performace, but in releasing new features. Since that was not a good thing in todays fast and modern world, when almost every week new hot thing comes up, it has had to change. And fortunatelly - it does. Now releases are much, much more frequent. And with Eclipse Foundation taking care of Java (or should I say Jakarta) the future looks bright.

But Java SE/EE is nothing more than just a set of specifications. You cannot do a thing with it. You need application server. And this post is all about setting up a basic project with Wildfly, the Redhat's free, widely used and highly customizable application server.

Since it is easy now to miss out on new stuff, I am going to user newest Wildfly, which is 17 (Java EE 8 support) with JDK 12 to create simple service.

Whole code can be found right here: https://github.com/KKucharczyk/javaee8-bootstrap

Requirements

To setup simple application, couple of things are required:
As an IDE I am using Eclipse 2019-06 (4.12.0), although any (NetBeans, VS Code) will do, even vim is enough :) But since I was playing with Eclipse, I would like to also point out some errors/problems I faced.

Setting up

First things first, as Bruce Robertson in Filth said. Maven, the one to build it all.
I am Ubuntu user, so for me it was just sudo apt install mvn. It is probably as easy with other OS, so I am not going into details. To check if maven is working, simply check its version:

The important part here is the version of Java. To set it with ease, I advise setting JAVA_HOME variable on your system. I usually just keep it in .bash_rc or .zshrc depending on shell I am using. It is quite important to set it to your JDK 12, since otherwise maven will complain when compiling.

Next, let's create the project with:
mvn -B archetype:generate -DgroupId={groupId} -DartifactId={artifactId}

This command will create very simple maven project in standard convention. With one app class and one test, it's a good start. This project can be now easily imported to IDE. Alternatively, any IDE wizard will do. I am used to using console, but the same will be achieved for example by selecting Simple Archetype in New -> Maven project in Eclipse IDE.

With such a setup, let's create an endpoint.

Shall we talk with you, application?


I have created two classes - RestDemo, responsible for communication and DemoDto, which is a simple data I would like to transfer and updated the pom.xml.
So what is actually going on in here? Let's see step by step:

  • pom.xml - I have added properties to inform maven to compile this project with JDK 12. It can also be set via compiler-plugin, but I find it more clean this way. Also set sourceEncoding to UTF-8. The javax dependency is the Java EE 8 implementation. Since Wildfly provides me with this implementation, I set scope to provided
  • DataDto - simple Data Transfer Object, which will be returned with my rest service. Important thing here is to set it to public. Otherwise it won't be visible.
  • RestDemo - the "connector" between server and the world. I have set it on path "demo", it will produces JSON data and also it's lifetime is only per request.
To run the application, simply move created .war file Wildfly deployment file. Although it will deploy, it won't work. As it said:

WARN  [org.jboss.as.jaxrs] (MSC service thread 1-5) WFLYRS0015: No Servlet declaration found for JAX-RS application.  In bootstrap.war either provide a class that extends javax.ws.rs.core.Application or declare a servlet class in web.xml.

Yep, the JAX-RS must be activated. It can be done in two ways, but I consider servlet a thing of a past, so I will do it the easy way, which is adding such a class:

All right, deploying app now should simply register context /api, as said:

RESTEASY002225: Deploying javax.ws.rs.core.Application: class krzysztof.javaee8.config.JaxRsActivator
WFLYUT0021: Registered web context: '/bootstrap' for server 'default-server'

Now if I get to the url: http://localhost:8080/bootstrap/api/demo, I see my awesome Dto: {"message":"Message"}.
Url is a combination of my app name, JAX-RS's application path and endpoint path.
And that's it. Easy to setup and begin writing the business logic.


To guide the lost

When playing with Eclipse IDE I have stumbled across yet another problem - after setting my instane of Wildfly 17 in Server tab I could not actually add a resource to it, to deploy it from Eclipse IDE. As it turns out, my project had wrong facet. To fix it I chose my project with properties -> Project Facets -> Converted project to facet and chose:

  • Java with Version: 12
  • Dynamic Web Module (which is required for next chosen element)
  • JAX-RS
Apply and now it can be chosen and finally - deployed.

Komentarze

Popularne posty z tego bloga

Testing: jUnit's TemporaryFolder

Two entities coexisting in one table