In JAX-RS, we can map exceptions to a Response. This will be useful in hiding the exceptions from the client of JAX-RS web services. Below is an example showing how to map exceptions to appropriate responses. First we need to implement ExceptionMapper interface as shown below. Here we want to map NullPointer exception to Internal… Continue reading Mapping exceptions to Response in JAX-RS
Configuring the Jsonb instance
From the previous posts we can see that an instance of Jsonb is used for serialization/deserialization of java objects to/from json format. We can configure the Jsonb instance by creating an instance of JsonbConfig. By default jsonb implementation provider must support the following properties to be configured. jsonb.to.json.formatted – java.lang.Boolean Controls whether or not the… Continue reading Configuring the Jsonb instance
Serialization of null values when converting from java object to json format
In this post I will explain how serialization of null values feature can be turned on or off. Below is the class structure of the object to be serialized Student 1 import javax.json.bind.annotation.JsonbProperty; 2 3 public class Student { 4 private int id; 5 private String name; 6 private Integer rollnum; 7 8 public int… Continue reading Serialization of null values when converting from java object to json format
Preventing an property from being serialized to json
By default when an object is serialized to json format, all the properties in the object are serialized. We cannot prevent this default behavior, by annotating the property with @JsonbTransient annotation. Below is the example. The class to be serialized is Employee Employee 1 import javax.json.bind.annotation.JsonbTransient; 2 3 public class Employee { 4 private int… Continue reading Preventing an property from being serialized to json
Deserialization of java objects present in JSON format from a file
This post will explain how to read the serialized (JSON) form of Java objects from a file. Person public class Person { private String fName; private String lName; private int age; public String getfName() { return fName; } public void setfName(String fName) { this.fName = fName; } public String getlName() { return lName; } public… Continue reading Deserialization of java objects present in JSON format from a file
Serialization of java object in JSON format to a file
This post will explain how to save the serialized (JSON) form of Java objects to a file. Main Code import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.json.bind.Jsonb; import javax.json.bind.JsonbBuilder; public class JsonbDemo2 { public static void main(String[] args) throws IOException { Person person = new Person(); person.setfName(“fName”); person.setlName(“lName”); person.setAge(10); Jsonb jsonb = JsonbBuilder.create(); File file… Continue reading Serialization of java object in JSON format to a file
Logging to database using java logging framework
This post explains how to log messages to the database. Create a table with below schema CREATE TABLE logging ( loggername VARCHAR(50) NULL DEFAULT NULL, threadId INT(11) NULL DEFAULT NULL, loglevel VARCHAR(50) NULL DEFAULT NULL, logmessage VARCHAR(50) NULL DEFAULT NULL ) Create a custom handler which will log the message to the database. In our… Continue reading Logging to database using java logging framework