Java List throws UnsupportedOperationException

Typically, we use Arrays.asList or the new Java 9 List.of to create a List. However, both methods return a fixed size or immutable List, it means we can’t modify it, else it throws UnsupportedOperationException. JavaListExample.java package com.mkyong; import java.util.Arrays; import java.util.List; public class JavaListExample { public static void main(String[] args) { // immutable list, cant …

Read more

What is java.util.Arrays$ArrayList?

The java.util.Arrays$ArrayList is a nested class inside the Arrays class. It is a fixed size or immutable list backed by an array. Arrays.java public static <T> List<T> asList(T… a) { return new ArrayList<>(a); } /** * @serial include */ private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable { private static final long serialVersionUID = …

Read more

JUnit 5 + AssertJ examples

In this article, we will show you how to write test assertions with AssertJ. P.S Tested with JUnit 5.5.2 and AssertJ 3.14.0 pom.xml <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.5.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>3.14.0</version> <scope>test</scope> </dependency> 1. JUnit 5 assertions to AssertJ It’s easy to convert JUnit 5 assertions to AssetJ, see the following syntax: JUnit …

Read more

JUnit 5 Expected Exception

In JUnit 5, we can use assertThrows to assert an exception is thrown. P.S Tested with JUnit 5.5.2 1. Unchecked Exception 1.1 JUnit example of catching a runtime exception. ExceptionExample1.java package com.mkyong.assertions; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class ExceptionExample1 { @Test void test_exception() { Exception exception = assertThrows( ArithmeticException.class, () -> divide(1, 0)); assertEquals("/ …

Read more

Spring @ExceptionHandler and RedirectAttributes

Since Spring 4.3.5 and 5.0 M4, it supports RedirectAttributes argument in the @ExceptionHandler method. @ExceptionHandler(MyCustomException.class) public String handleError1(MyCustomException e, RedirectAttributes redirectAttributes) { redirectAttributes.addFlashAttribute("message", "abcdefg"); return "redirect:/viewName"; } @ExceptionHandler(MultipartException.class) public String handleError2(MultipartException e, RedirectAttributes redirectAttributes) { redirectAttributes.addFlashAttribute("message", e.getCause().getMessage()); return "redirect:/viewName"; } P.S Read this SPR-14651 Noted If you are using Spring < 4.3.5, do not add ...

Read more

Spring MVC – How to handle max upload size exceeded exception

In Spring, you can declare a @ControllerAdvice to catch the ugly max upload size exceeded exception like this : Solution Depends the types of multipartResolver : StandardServletMultipartResolver – catch MultipartException, refer to this example. CommonsMultipartResolver – catch MaxUploadSizeExceededException – refer to this example. GlobalExceptionHandler.java package com.mkyong.exception; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.multipart.MultipartException; import …

Read more

Java Custom Exception Examples

In Java, there are two types of exceptions – checked and unchecked exception. Here’s the summary : Checked – Extends java.lang.Exception, for recoverable condition, try-catch the exception explicitly, compile error. Unchecked – Extends java.lang.RuntimeException, for unrecoverable condition, like programming errors, no need try-catch, runtime error. 1. Custom Checked Exception Note Some popular checked exception : …

Read more

Spring MVC – Catch the exceptions thrown by view page

Here’s the scenario, the controller returns a ModelAndView, and an exception is thrown while rendering the JSP view page, reason behind is one of message code is not found. org.apache.jasper.JasperException: org.springframework.context.NoSuchMessageException: No message found under code ‘Diff.userform.password’ for locale ‘en_US’. org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) An exception is thrown and render an HTTP 500 error …

Read more

java.lang.ClassNotFoundException: org.hibernate.service.jta.platform.spi.JtaPlatform

Spring 3.2.x + Hibernate 4.3.x integration, hits JtaPlatform ClassNotFoundException, search the project classpath, find out that JtaPlatform is at different package? hibernate-core.4.3.5.Final.jar org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform Error message shows org.hibernate.service.jta.platform.spi.JtaPlatform Caused by: java.lang.NoClassDefFoundError: org/hibernate/service/jta/platform/spi/JtaPlatform at org.springframework.orm.hibernate4.SpringSessionContext.<init>(SpringSessionContext.java:56) ~[spring-orm-3.2.8.RELEASE.jar:3.2.8.RELEASE] … 40 common frames omitted Caused by: java.lang.ClassNotFoundException: org.hibernate.service.jta.platform.spi.JtaPlatform //… pom.xml <!– Hibernate ORM –> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.5.Final</version> </dependency> <!– …

Read more

Spring MVC @ExceptionHandler Example

In this tutorial, we show you how to do exception handling in Spring MVC frameworks. Normally, we use @ExceptionHandler to decide which “view” should be returned back if certain exception is raised. P.S This @ExceptionHandler class is available since Spring 3.0 1. Project Structure Review the project directory structure, a standard Maven project. 2. Custom …

Read more