Creating Spark API with Java

It is not a secret that the Java approach to enterprise web applications has generated opinions. On the one hand, Java EE is ideal for applications such architecture, because the server provides important features such as security; without the developer should worry about that. However, this also causes it more complicated to develop lighter projects ;because it requires several configurations to make it work.

For this reason, there have been several initiatives to prevent Java’s popularity on the web decreases . Currently, it ranks third in popularity of server side languages. Below PHP and ASP .NET and above languages like Python and Ruby.

These initiatives do not suggest that is set aside for Java EE; but they seek to meet the needs of niche enterprise web applications.

On the other hand, it also emerged that seek to solve other less demanding needs a more agile; and that is the case the framework Spark Java . It is a lightweight web framework inspired by Sinatra (framework based on Ruby), which allows you to create web applications with Java 8 with the minimum effort. Spark API with Java

Spark API with Java
Spark API with Java

The main objective of this article is to introduce the framework and create a simple solution that allows you to understand how you could make use of it. To continue the tutorial you need to have installed the Java Development Kit 8 (JDK 8) . It can be installed on Windows, Mac or Linux and download the official website of Oracle . Spark API with Java

Additionally, you will require a development environment (IDE) for Java that allows you to create a project Maven . You can use Eclipse, IntelliJ or NetBeans. In my case, I will use the version 8.0.2 of NetBeans for Java EE .

And if you’re starting to develop applications with this language, you can not miss the free Java Basic course in Platzi. You will learn everything you need to know to create projects from scratch.

What is Spark?

It is a micro web framework based on Java Sinatra , whose total size does not exceed 1 MB. In order to maintain a clean code, developers decided to stop supporting Java 7 and take advantage of the simplicity offered Java 8 . For this reason, Spark 2 can only be developed with this version of the language.

In the images below you can see the change that was in the Hello World of Spark Spark 1 to 2.

Hello World in Spark 1: Spark API with Java

Spark API with Java

Hello World in Spark 2

Spark API with Java

What Spark used? Spark API with Java

Although Spark is based on Sinatra, it is not presented as a competition. Its purpose is to have an alternative for Java developers, enabling them to quickly develop web applications with the language they already know.

According to a survey conducted by the Spark team this year, it announced that about 50% of users use it to create Spark REST APIs and 25% use it to create web pages . They also mention that 15% of applications deployed Spark serve over 10,000 users daily. With these data we can conclude that it is an appropriate framework if the purpose is to build a Java API with an agile.

Creating a Hello World: Spark API with Java

After installing the JDK 8 and Netbeans for Java EE, we will open the last and create a new project File> New project . In the dialog select the folder Maven and choose Java Application.  Then select the option Next .

Spark API with Java
Spark API with Java

In the next dialog enter the name of the project and we click Finish .

In this way,  NetBeans will create a project with the structure of a Maven project .The elements on which work will be located in the package Source Packages and the file pom.xml   located in Project Files .

Spark API with Java
Spark API with Java

If we open the file pom.xml find content similar to the following:

  1. <? xml version = “1.0” encoding = “UTF-8” ?>
  2. <project http://maven.apache.org/xsd/maven-4.0.0.xsd ” >
  3. <modelVersion> 4 . 0.0 </ modelVersion>
  4. <groupId> com. peewah </ groupId>
  5. <ArtifactId> holaplatzi </ artifactId>
  6. <version> 1. 0 -SNAPSHOT </ version>
  7. <Packaging> jar </ packaging>
  8. <Properties>
  9. <project. build . sourceEncoding > UTF – 8 </ project. build . sourceEncoding >
  10. <maven. compiler . source > 1. 7 </ maven. compiler . source >
  11. <maven. compiler . target > 1. 7 </ maven. compiler . target >
  12. </ Properties>
  13. </ Project>

What we do next is add Spark dependence file to install Maven in the project. Our filepom.xml would read :

  1. <? xml version = “1.0” encoding = “UTF-8” ?>
  2. <project http://maven.apache.org/xsd/maven-4.0.0.xsd ” >
  3. <modelVersion> 4 . 0 . 0 </ modelVersion>
  4. <groupId> com. peewah </ groupId>
  5. <ArtifactId> holaplatzi </ artifactId>
  6. <version> 1.0 -SNAPSHOT </ version>
  7. <Packaging> jar </ packaging>
  8. <Properties>
  9. <project. build . sourceEncoding > UTF – 8 </ project. build . sourceEncoding >
  10. <maven. compiler . source > 1. 7 </ maven. compiler . source >
  11. <maven. compiler . target > 1. 7 </ maven. compiler . target >
  12. </ Properties>
  13. <- Maven dependencies ->
  14. <Dependencies>
  15. <- Unit Spark Java ->
  16. <Dependency>
  17. <groupId> com. sparkjava </ groupId>
  18. <ArtifactId> spark-core </ artifactId>
  19. <version> 2 3 </ version>
  20. </ Dependency>
  21. </ Dependencies>
  22. </ Project>

Maven to install the library, we must right – click the project and select the Build . After the compilation, our project will have installed Java Spark.

Now that we set you need to add Spark Java project, we will write the necessary code. To achieve this we right click on the package Source Packages , select New and then Other .

Spark API with Java
Spark API with Java

In the dialog select the folder Java;  we choose Java Class and we click on Next .

Spark API with Java
Spark API with Java

In the following dialogue we write the name of our class, HolaPlatzi in this case, and select  Finish.

Spark API with Java
Spark API with Java

NetBeans creates us our class and placed inside the package we have previously created.The initial code should be something like this:

  1. package com. peewah . holaplatzi ;
  2. / **
  3. *
  4. * @author Csacanam
  5. * /
  6. public class HolaPlatzi
  7. {
  8. }

Then we add the need to create a web service type code GET that returns a message that can see in the browser. To achieve this we must create the method main , which is the entry point into a Java application. Within  main Web service type is added GET .

In the line of code where we define it , you need to specify  the type, path (/ hello), the parameters req (request) and response (res) and finally, the message to be displayed in the browser. Thus, the class created above should look like: Spark API with Java

  1. package com. peewah . holaplatzi ;
  2. import static spark. Spark . *;
  3. / **
  4. *
  5. * @author Csacanam
  6. * /
  7. public class HolaPlatzi
  8. {
  9. public static void main ( String [ ] args )
  10. {
  11. get ( “/ hello” , ( req, res ) -> “Hello Platzi Greetings from Peewah..” ) ;
  12. }
  13. }

If NetBeans indicates that there is an error by lambda expressions , we must right -click the project and select Properties . In the category Sources we must change the option Source / Binary Format 1.8. This is only available if you have previously installed the JDK 8.

To run the project, we chose the option to run the project on the button at the top or pressed F6. By doing this, a dialogue which will indicate that we select the main class (so far we have only created one) will open. After this, we press the option Select Main Class .

Spark API with Java
Spark API with Java

After a few seconds the console shows us a similar message shown below, indicating that the server is running on port 4567. This is the default port of Spark Java . However, we can modify it if you wish.

Spark API with Java
Spark API with Java

To test our web service is running, we open your browser of choice and put the address http: // localhost: 4567 / hello . The result browser will show something like this :

Spark API with Java
Spark API with Java

And this is how we create easily and quickly a web service with Spark Java that returns a greeting message. If you are interested to know more and learn about the potential of this web framework and also learn to read and write code in Java, good programming practices, object – oriented paradigm and all the elements you need to program from scratch Spark API with Java

Leave a Reply