JAVA intro: Introduction to Maven and JSON Parsing

 In Self-Development, Tech Corner

In this article we will go through Maven installation, environment setup and building a sample project for JSON parsing. If you are familiar with Maven already you can skip to the JSON processing part.

MAVEN INSTALLATION STEP BY STEP

Maven is a project management tool that enables building big and scalable apps, which make use of multiple libraries and modules. Maven can be installed by downloading the zip file from its official website and simply setting the environment variable to the home directory of the unzipped contents. For Windows, the environment variables can be found by simply searching in the windows bar for “environment variables” and then adding a new system variable named M2_HOME. After that we need to find the Path variable from the list of system variables, click “Edit” and add the following value:

%M2_HOME%\bin.

For Linux/Unix systems, the maven path variable can be set by executing the following command:

> export PATH=/example/maven/bin:$PATH

The installation can be verified by simply opening the command line and typing “mvn -version”. It can be used both in Eclipse and in Intelij’s IDEA. The Eclipse IDE must be an Eclipse IDE for Java EE (enterprise edition).

In order for Maven to run, the system needs to have installed the java development kid (JDK) which can be downloaded from the Oracle official site or some other distribution of JDK, like AdoptOpenJDK or other vendors that don’t require subscription or licensing. Java can be set the same way as Maven i.e. editing the environment variables (adding a new one named JAVA_HOME and editing the path variable).

CREATING YOUR FIRST MAVEN PROJECT

First we need to create a new Maven project and this can be done by only clicking on the “New” menu both in Eclipse and in InteliJ. For each Maven project you need to have group id(org.example.testproject) and artifact id(testapp). After that by clicking the “Finish” button the project will be created. The group id is commonly used on a company level, so that everyone in that company can find the artifacts and re-use them if needed.

The pom.xml file has been created along with the project. This is the file that Maven uses in order to resolve all the dependencies for the project. We can create new maven modules and add them as a dependency in the pom file, but also we can simply import the libraries (artifacts) that we will need. Maven downloads the packages needed for the app, before the app itself is started.

The Maven project has a class named App that defines the app’s behaviour on startup. For starters we will use the App class to run the first sample project. As mentioned above, the project will be used for parsing JSON. We can parse json by using Jacskon, Gson or simply Json.

  • Jackson
  • Gson (Google Json)
  • JSON

Each of these libraries is free to use and you can add it to you project by simply searching on the maven official site for the library that we need and after finding it, we just copy the dependency to our pom.xml file. In order for the dependencies to be resolved the project needs to be built first. To do so, run the Cmd and navigate it to the directory where the Maven project is located. For windows, this can also be done by navigating first to the Maven project on the file system and then typing “cmd” in the address bar. This will open the cmd and it will point to the projects directory. Next, type the following command:

> mvn clean install

This will build the project and it will output “Success” on the command line output, if no errors occurred during the build.

JAVA JSON PARSING WITH JACKSON

Jackson is a high performance JSON processor which can easily convert a Java object to a JSON string and vice versa. JSON is short for Java Script Object Notation. Most of the communication between front-end and back-end is designed to work with JSON objects, because it is convenient both for the back-end and front-end.

In order to use the Jackson library we need to add the Jackson dependency in the pom.xml under the <dependencies> tag. If there is no dependencies tag, you need to create it and then simply copy the dependency from the Maven’s official website and rebuild the project with the command mentioned above.

mavens-jackson-dependency

The other option to use the Jackson library for JSON processing is to download the jar file and add it to your Java project as an external library. In order to verify whether the library has been imported properly in the project, we can import the ObjectMapper class and if that is successful, the project setup has been completed.

import com.fasterxml.jackson.databind.ObjectMapper;

CONVERT JAVA OBJECT TO JSON USING JACKSON

Each Java object can be transformed to its JSON representation, which will output a variable of type String. This is accomplished by using the ObjectMapper class, provided by Jackson.

//converts the java object to a json string
Example example=new Example();
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(example);

 

The last line in the example above can be replaced with the following line, which will also output a JSON string, but in this case it will be formatted by default i.e. without the pretty printer output.

String jsonString =mapper.writeValueAsString(example);

The example class is a simple Java class:

public class Example{
private String name;
private Integer exampleId;
private Double exampleDouble;
//getters and setters
...
}

The class must provide getters and setters in order for the ObjectMapper to work properly.

CONVERT JSON STRING TO JAVA OBJECT USING JACKSON

In order to convert a JSON string to a Java object, we will use the ObjectMapper class again. The class that we want to transform the string to must provide setters and a default constructor (no arguments). If no other constructors are provided in the class the default constructor can be also left out, since the Java compiler will create it. But if you have a constructor with one or more arguments, the default constructor must be provided as well.

ObjectMapper objectMapper = new ObjectMapper();
Example resultExample = objectMapper.readValue(jsonString, Example.class);

The jsonString variable used in the example above must be in a JSON format so that the object mapper can convert it to a Java object i.e. to the Example class. A simple JSON string would look like:

{"name":"Example", "exampleId":234, "exampleDouble":2332.44}

If you want to use this string in the project you will need to add escape characters so that Java can read the string properly:

{\"name\":\"Example\", \"exampleId\":234, \"exampleDouble\":2332.44}
Recommended Posts

Leave a Comment

VR

SINGULAR STORY IN YOUR INBOX

Get news about career opportunities and product updates once a month.