Java try-with-resources with Example
Since Java 7, the try-with-resources statements allow declaring AutoCloseable resources in a try block that JVM will close automatically.
Since Java 7, the try-with-resources statements allow declaring AutoCloseable resources in a try block that JVM will close automatically.
1. Reason The ObjectNotFoundException exception is thrown when we try to load an entity in hibernate with session.load() method and entity is not found. The exception trace looks like this: 2. Solution The correct way to solve this problem is to use session.get() method. The get() method will return null …
Polymorphism in Java is the ability to create member functions or fields that behaves differently in different programmatic contexts.
Java collection interview questions. These questions are most asked interview questions for beginners to medium level developers in Java interviews.
The Path class, introduced in the Java SE 7 release, is one of the primary entry points of the java.nio.file package. If our application uses Java New IO, we should learn more about the powerful features available in this class. In this Java tutorial, we are learning 6 ways to …
Learn to configure and use the Ehcache library as the second-level cache provider for our hibernate application with an example.
1. Reason While working on post “Named queries in hibernate“, I encountered this error. Hibernate entity I coded was like this: And the error was: 2. Solution The problem is with the entity names in most of the cases. Actually, we have to follow HQL syntax, and it tells us …
Learn to create, group and execute named HQL and named native SQL queries in Hibernate using @NamedQuery and @NamedNativeQuery annotations.
The hibernate second-level cache is separate from the first-level cache and is available to be used globally in SessionFactory scope.
The first level cache (enabled by default) helps Hibernate to reduce the number of queries made to the database to improve the performance.
Learn to insert an object into the database using hibernate’s Session interface, JPA’s EntityManager and HQL Query interface with examples.
Security is an integral part of any enterprise application. Security involves two phases i.e. authentication and authorization. Authentication verifies who you are. Authorization verifies what you are authorized to do. In this post, we will learn to build role based basic authentication/ authorization security for REST APIs. Sections in this …
Learn to read and write excel files in Java using Apache POI library – with examples. Learn to add and evaluate formula cells and add color formatting also.
HashMap interview questions and ConcurrentHashMap interview questions. Learn questions like how hashmap works, rehashing in concurrenthashmap and more.
Let us identify 8 such warning signs, which will provide us with subtle hints that a particular test/suit should be improved.
Learn SOLID principles in Java with examples. Single Responsibility Principle, Open Closed Principle, Liskov’s Substitution Principle, Interface Segregation Principle, Dependency Inversion Principle.
This Jersey tutorial will help you set up the Jersey environment in your local machine and get started with a simple Hello World REST API.
ETags or entity tags are useful HTTP headers which can help in building a super fast application by minimizing the server load on system. ETag is set to the response to the client so a client can use various control request headers such as If-Match and If-None-Match for conditional requests. …
I have been facing this issue from quite long time as I didn’t took care of it. Everytime I faced this issue, I just went and changed the project compiler level for projects facets menu in eclipse. Today, I decided to end this for all. This error looks like in …
If you have started working on Jersey then you might face this issue while configuring it. This is the result of insufficient project dependencies on runtime. This issue is likely to be faced in the tomcat server. The Error The error log will look like this. The Solution To solve …
ToStringBuilder is a utility class provided by apache commons lang library. ToStringBuilder is a utility class provided by apache commons lang library. It provides a consistent and better control over what and how much data, an object should expose using toString() method and in which format. It can also help …
Learn to create and handle custom exceptions using resteasy ExceptionMapper interface implementations. It must be annotated with @Provider to work.
Launch4j is a cross-platform tool for wrapping Java applications distributed as jars in lightweight Windows native executable files. In this post, we will learn making such an executable file for a demo java application. Step1 ) Create a java application I am creating a very basic java class which simply …
The Bean Validation API (JSR-303) defines a meta-data model and API for bean validation based on annotations. The functionality is achieved through the resteasy-hibernatevalidator-provider component. In order to integrate, we need to add resteasy-hibernatevalidator-provider.jar and hibernate-validator.jar files to the classpath. If you are using maven the, you need to add …
RESTEasy can generate a JavaScript API that uses AJAX calls to invoke JAX-RS operations. Learn to use JSAPIServlet with examples.
Excessive logging is a common cause of performance degradation of applications. It is one of the best practices to ensure proper logging within your Java EE application implementation. However, be careful with the logging level that you enable in your production environment. Excessive logging will trigger high IO on your …
Spring send email program for explaing the usage of javamailsender interface of javax.mail dependency. Send email example in Java using Spring framework.
In Java, there are different ways of reading and parsing CSV files. Let’s discuss some of the best approaches such as OpenCSV, Super CSV etc.
Use the constructor parameters wisely to reduce the number of unnecessary objects and improve the performance of ConcurrentHashMap.
Reason When you are working with java libraries that internally depend on cglib, then it’s very normal to encounter the below error: This error is due to an incompatible version of cglib with additional jar files in your project, such as asm. Solution The solution is very easy. Just place …
In this example, I will show the usage of ValidatorAdapter in association with @ValidateRequest annotation. For sending the request from UI, I will use ajax. You can use form submission if needed in your project. In that case, you will need @FormParam annotation to capture request parameters. Below if the …
In previous post, we learned about uploading a file in JAX-RS RESTEasy application where the client to upload the file was built using HttpClient library. This client was pure java client and does not have any UI associated. In this post, I am building the same uploading feature but this …
In previous posts, we learned about file downloading and building RESTful clients. Now, let move further. In this post, I am giving sample code of file upload using jax-rs resteasy. For uploading the file, httpclient library will be used instead of HTML form. I am using MultipartFormDataInput class which is …
So far in this blog, we have been learning about building RESTful webservices which are server side components. In this post, we will learn to build a RESTful client for consuming the webservices written in previous posts. I will be re-using the code base written for RESTEasy + JAXB xml …
Apache HttpClient simplifies the interactions with RESTful APIs by supporting various HTTP methods, headers, and request configurations. This article will explore how to consume REST APIs using Apache HttpClient5 with examples. 1. Setup Before diving into implementation, we need to import the latest version of Apache HttpClient. Next, we will …
Java 8 contains the new Date and Time APIs/classes (JSR-310), called ThreeTen, which will change how you have been handling dates in Java.
Learn to use ResteasyProviderFactory to share data between multiple classes and interceptors in a RESTEasy based web application.
Learn to use JAX-RS @Path annotation and write resource path in it for URI matching. Paths can be written in simple form (static or path parameter) or complex form (regex based). 1. JAX-RS @Path URI Matching Types JAX-RS @Path annotation is used to specify the URI on which the resources …
RESTEasy is JBOSS provided implementation of JAX-RS specification for building RESTful Web Services and RESTful Java applications. RESTEasy works in combination with HTTP media types for providing the response in specific formats such as images, pdf or text. The core component to configure for enabling support for multiple media types …
So far in this blog, we have been learning about building RESTful webservices which are server side components. In this post, we will learn to build a RESTful client for consuming the webservices written in previous posts. I will be re-using the code base written for RESTEasy + JAXB xml …
Learn to use Jackson or Jettison libraries with RESTEasy to convert the API response into JSON format with example.
Learn to use Jackson with RESTEasy to convert the API response into XML format with example.
RESTEasy example web application to to create REST APis using maven and tomcat server. This resteasy tutorial helps in create restful webservices.
The spring bean autowiring functionality has four modes. These are ‘no‘, ‘byName‘, ‘byType‘ and ‘constructor‘. Autowiring can be in classes with @Autowired.
Spring bean autowiring “byConstructor” is the recommended approach and injects dependencies into a bean by utilizing its constructor.
In Spring Framework, autowiring by name is a feature that allows developers to inject dependencies into a bean by matching the property name with the name of the target bean. This is achieved using the @Autowired annotation (Java Configuration) along with the byName attribute (XML Configuration). 1. What is Autowiring …
Bean autowiring by Type instructs the Spring IoC container to inject a bean of the same type as the property into which it is being injected.
Explore the Spring bean lifecycle, post-initialization and pre-destruction callbacks, the best practices and potential pitfalls with examples.
The beans in Spring or Spring Boot can be created in six different scopes: singleton, prototype, request, session, application and websocket.
Learn to design a good custom key class for HashMap. As a rule, always honor the hashcode and equals contract, and make the class immutable.
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.