Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Spring MVC - How to handle errors from a controller?

In a web application, it is very important to handle errors. Proper error handling would protect the application from several vulnerabilities, including Security Misconfiguration. In this post, you will see how you can handle exceptions that arise out of a Spring MVC based application.

Java - How to find the IP Address of a host in Java ?

In Java, if you are looking to resolve an IP Address from the given hostname, then you can leverage the capabilities provided by InetAddress. It belongs to the "java.net" package and provides various utilities related to resolving addresses and IPs.

Localization with Spring

Localization enables applications to cater to users of different locations and languages. Spring, as usual, has support for this aspect as well.

Before jumping into what Spring has to offer, let's explore what we get from Java itself.

Deep Copy Java Objects through Serialization

When it comes to deep copy or cloning an object, the first thing that comes to mind is to override the clone() method and set each field manually. This can be pretty cumbersome to implement for complex objects or if you have to do this for a lot of them.

A simpler way to deep copy an object is to serialize it and then deserialize to get a new instance.
Let's check out a few examples.

Deep Copy using Spring's SerializationUtils


//import org.springframework.util.SerializationUtils;

 byte[] sourceInBytes = SerializationUtils.serialize(sourceInstance);
 Object newInstance = SerializationUtils.deserialize(sourceInBytes);

Since we are using java serialization here, the Class needs to implement Serializable.

Deep Copy using Apache Commons Lang SerializationUtils

//import org.apache.commons.lang.SerializationUtils
 Object newInstance = SerializationUtils.clone(sourceInstance);

Just like above example, this one needs the POJO to implement Serializable.

Deep Copy through JSON serialization

//import org.codehaus.jackson.map.ObjectMapper;
//import org.codehaus.jackson.map.ObjectMapper.DefaultTyping;
 
 ObjectMapper mapper = new ObjectMapper();
 mapper.enableDefaultTyping(DefaultTyping.NON_FINAL);
 String jsonSource = mapper.writeValueAsString(sourceInstance);
 MyClass newInstance = mapper.readValue(jsonSource, MyClass.class);

The advantage with JSON is that the Class need not implement a Serializable interface. It just needs to be a POJO with a public no-arg constructor and getters/setters for the properties. enableDefaultTyping here ensures that the Polymorphic Type Handling is taken care of.

JSON Property Name Customization in Jackson using PropertyNamingStrategy

Jackson is one of the most popular java libraries for serialization/deserialization of POJOs to/from JSON. By default, Jackson derives the JSON element names from the getter/setter method names of the POJO. e.g. getActorName() is translated to actorName in the resulting JSON.