My Articles:
Knowledge of how to secure REST APIs is as much necessary as writing the APIs themselves. Mostly REST APIs are HTTP protocol-based, and any user having an internet connection can access them, and so can bad users as well. It is crucial to write secure APIs to protect the business. …
Learn to use hibernate initialize() method and enable_lazy_load_no_trans property to force initialization of proxy entity or collection.
If you are working on Hibernate and HSqlDB, and you are trying to fetch collection of entities, then you may face this error. For single entity you may or may not find this error. Exception will look like this: Solution This error is due to version mismatch of hibernate and …
If you have worked on javascript then you must have noticed the equality and identity operators to compare objects or values. Many developers do not understand the correct version they use in specific scenarios. The correct decision is based on the knowledge of how they work. Let’s understand. 1. Difference …
This Jackson tutorial will teach us to use Jackson ObjectMapper to read and write JSON data into Java Objects. 1. Setting Up Jackson To include Jackson library in our project, we should include jackson-databind dependency which internally pulls the other two needed dependencies i.e. jackson-annotations and jackson-core. We can find the latest …
Concurrency means multiple tasks running in overlapping time periods. Parallelism is when several parts of a unique task run at the same time
This tutorial gives an example of implementing a Stack data structure using an Array. The stack offers to put new objects on the stack (push) and to get objects from the stack (pop). A stack returns the object according to last-in-first-out (LIFO). Please note that JDK provides a default java …
We may need to design our own custom list implementation for a very specific requirement or simply as a coding question in a Java interview.
Quicksort follows the divide-and-conquer approach and works by dividing the input array into two sub-arrays, then recursively sorting each sub-array before merging.
Java maintains a Set of system properties that can be accessed in the runtime by executing programs. Each system property is a key-value pair. For example, one such system property is “java.version”=”1.7.0_09“. We can retrieve all the system properties via System.getProperties() or we can also retrieve individual property via System.getProperty(key) …
Tomcat directory listing allows the users to see a list of files and directories when they navigate to a directory. Learn to enable or disable this feature.
Learn to create new date, get current date, parse date to string or format Date object using java.util.Date class. These use-cases are frequently required, and having them in one place will help in saving time for many of us.
Java offers many useful ways to get current date or current time using Date, Calendar and newly introduced LocalDate, LocalDateTime and ZonedDateTime classes in Java 8 Date/Time API classes.
One of our reader, Anant, asked this extremely good question to elaborate / list down all related topics that we should know about multi-threading including changes made in java 8.( Beginner level to Advance level). All he wanted to know was evolution of Multi-threading Framework in Java from Simple Runnable …
You may have faced this question in your interview that what is the difference between lock and a monitor? Well, to answer this question you must have good amount of understanding of how java multi-threading works under the hood. Short answer, locks provide necessary support for implementing monitors. Long answer …
Although SOAP and REST- both help building applications based on SOA, yet they are pretty much different. Let us learn few differences.
Which logging framework we should use in our new application? I have been asked this question multiple times, so I thought to write down my answer in this post so fellow developers can refer to it from time to time when needed. 1. SLF4j acts like abstract Logging API Simple …
Learn to find difference between two dates in days, months or any other time units – using Java 8 classes such as Duration, ChronoUnit and finally JodaTime.
We must have heard multiple times that enums are always the best choice for implementing singleton design pattern in java. Are they really the best? If it is then how is it better than other available techniques? Let’s find out. Writing a singleton implementation is always tricky. I already discussed …
Java enumerations are only applicable for legacy classes e.g HashTable, and Vector. Iterator is the recommended way to iterate over collections since Java 1.2 and takes place of Enumeration.
The given Java program uses a TreeMap to store the length of words as Map key, and all similar length strings in a List as Map value.
1. What is a Complete String? A string is said to be complete if it contains all the characters from a to z. If it misses even a single character, it is considered an incomplete string. For example, the following string is a complete string. It contains all the characters …
These tips will help you devise and follow a practical and measurable performance optimization plan for a Java application.
Good strings do not have the same consecutive letters. We simply need to perform one operation – if there are two same consecutive letters, delete one.
When Java Iterator detects the modifications, it cannot guarantee iteration consistency and throws the ConcurrentModificationException.
Composite patterns allow clients to treat individual objects (under a hierarchical structure) in a uniform manner.
Bridge pattern is about preferring composition over inheritance. Implementation details are pushed from a hierarchy to another object with a separate hierarchy.
Learn to set cookies into HTTP responses sent by REST APIs. This example makes use of javax.ws.rs.core.Response for setting cookies into API responses.
In this example, we will learn to set cookies into HTTP requests invoked by the Jersey client. This example makes use of Invocation.Builder for setting cookies into outgoing REST calls. 1. Syntax The following is the syntax to set a cookie in a Jersey client request using the Cookie class …
By default, Jersey uses JUL for logging. Learn to use LoggingFeature and LoggingFilter classes to enable the additional logs of request and response.
Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and behavior from more than one parent object or parent class.
Learn to use Jersey REST client authentication using HttpAuthenticationFeature, which can be used to access REST APIs behind authentication security.
In this Jersey rest security example, we will learn to secure Jersey REST APIs with basic authentication. This will make mandatory every user to provide username/password to authenticate into portal. Also, user must have certain level of role as well. I have extended this example from my other example created …
This tutorial explains how to use google Gson with Jersey 2.x. There are some other java libraries also capable of doing this conversion e.g. MOXy, Jackson or JSONP; but Gson stands among very few which do not require any pre-annotated java classes OR sourcecode of java classes in any way. …
This tutorial explains how to use JSONP JSON provider with Jersey 2.x. JSONP is also auto-discoverable just like what we discussed in Jersey MOXy example. Table of Contents JSONP maven dependencies/changes REST API code Model bean changes Manually adding JsonProcessingFeature JSONP maven dependencies/changes JSONP media module is one of the …
This tutorial explains how to use MOXy JSON feature with Jersey 2.x. MOXy is the default JSON-Binding Provider in Jersey 2.x. Though I still personally prefer Jackson over MOXy for performance reasons. Table of Contents MOXy maven dependencies/changes REST API code Model bean changes Manually adding MoxyJsonFeature Customize behavior using …
In this example, we will create a jersey RESTful API web application using jersey-quickstart-webapp maven archetype in eclipse. Table of Contents Install remote archetypes in eclipse Create new maven project using jersey-quickstart-webapp Generated Files Run The Application Install remote archetypes in eclipse The very first step before creating actual maven …
By default in JAX-RS, ALL URLs specified in @Path annotations are case-sensitive. Learn to support case-insensitive URLs using regex and ResourceConfig.
This tutorial gives easy instructions for importing maven remote archetype catalogs in eclipse in easy steps.
Jersey 2 client API finds inspiration in the proprietary Jersey 1.x Client API. In this Jersey client example, we will learn to build client API and invode different REST methods and consule the API results.
In Jersey ExceptionMapper example, we will learn to handle custom exceptions using ExceptionMapper interface while developing Jersey RESTful web services. For demo purpose, I am modifying the sourcecode written for jersey download file example. Table Of Contents 1. Jersey custom exception with ExceptionMapper 2. How to throw exception from REST …
Learn to use Ajax with JAX-RS webservices (Jersey used in example) to upload multiple files with single button click. Also look at form based file upload example and file download example as well. Table of Contents Jersey maven multipart dependency Add MultiPartFeature Write Upload REST API HTML/Ajax code Demo of …
In this Jersey file upload example, learn to upload binary files (e.g. PDF files in this example) using Jersey’s multipart support for file upload.
In this Jersey file download example, we will learn to write a Jersey rest api which will be able to stream or download file (e.g. PDF/Excel/Text files) to requesting client. I will be using javax.ws.rs.core.StreamingOutput class for building this JAX-RS API. Table of Contents 1. REST API to stream file …
We can face this exception if we are setting up jersey 2 project first time in eclipse using the Maven plugin. The error message is simple enough to identify the root cause that the Jersey libraries are not in classpath. The Error The exception will look like this. Solution – …
Java Jsoup parses HTML. It provides a very convenient API for fetching URLs and extracting and manipulating data with examples.
Learn to create password-protected zip files using a very useful library Zip4j that uses completely Java code without any native code.
Learn to set environment variables e.g. JAVA_HOME when you don’t have admin access to your development windows machine.
Have you ever wondered how spell checkers, in any word editor, suggest you a list of probable other words whenever you have any spelling mistake?? This is done using phonetic search. Soundex is a phonetic algorithm for indexing names by sound, as pronounced in English. The goal is for homophones …
Learn to align a string to left, right or center in Java using the utility StringAlignUtils, which wraps all the alignment logic inside it.