Category Archives: Java

AsyncRequestTimeoutException in Spring Boot

If you using asynchronous programming with observable streams in spring boot, i suggest you to setup async request timeout because default async request timeout in spring boot 2.0 is 10 second. You will got an exception (AsyncRequestTimeoutException) when request is more than 10 second. AsyncRequestTimeout My suggestion is 60s or 120s (s mean second) : spring.mvc.async.request-timeout=60s That is how to… Read More »

Convert Array to List In Java 8

In java 8 you can easily to convert array to list with stream. Convert Array to List In Java 8 For example: String[] array = new String[10]; List<String> listCollection = Arrays.stream(array).collect(Collectors.toList()); ArrayList<String> arrayListCollection = Arrays.stream(array).collect(Collectors.toCollection(ArrayList::new)); Set<String> setCollection = Arrays.stream(array).collect(Collectors.toSet()); HashSet<String> hashSetCollection = Arrays.stream(array).collect(Collectors.toCollection(HashSet::new)); TreeSet<String> treeSetCollection = Arrays.stream(array).collect(Collectors.toCollection(TreeSet::new)); LinkedList<String> linkedListCollection = Arrays.stream(array).collect(Collectors.toCollection(LinkedList::new)); LinkedHashSet<String> linkedHashSetCollection = Arrays.stream(array).collect(Collectors.toCollection(LinkedHashSet::new)); ArrayDeque<String> arrayDequeCollection =… Read More »

REST API Return Image With Spring

How to create rest controller for return image file? Here is the code for create image rest API with spring: @GetMapping( value = “/img”, produces = {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_GIF_VALUE, MediaType.IMAGE_PNG_VALUE}) public ResponseEntity getImageAsResource() { HttpHeaders headers = new HttpHeaders(); String home = System.getProperty(“user.home”); File f = new File(home + File.separator + “Desktop” + File.separator + “image.jpg”); Resource resource =… Read More »

Download Image From URL In Java

Simple way to download image from url in java is like this: String source = “https://www.google.co.id/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png”; URL url = new URL(source); URLConnection urlConnection = url.openConnection(); InputStream in = urlConnection.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int n = 0; while (-1!=(n=in.read(buf))) { out.write(buf, 0, n); } out.close(); in.close(); byte[] response = out.toByteArray(); String home… Read More »

String to Byte Array in Java

To convert string to byte array in java you can use “getBytes” function. For example: “your string”.getBytes() String to Byte Array in Java See image below: That is how to convert string to byte array in java. In this example i’m using: IntelliJ IDEA Spring Boot Maven Other convert topics

IntelliJ IDEA – Create Spring Boot Application

This is how to create spring boot application from IntelliJ IDEA. 1. On the top IDE menu choose “File” -> “New” -> “Project…” 2. Setup JDK 3. Fill project details like group artifact type of build tools (maven or gradle) programming language (java, kotlin, groovy) packaging (jar or war) file application name application description package (com.example.module-name) 4. Choose… Read More »

How to Use Spring JdbcTemplate

This is how to use spring JdbcTemplate in spring boot application. In this example i’m using : Spring boot 2.0.2.RELEASE Maven 3.5.3 for build tools Java 1.8 How to Use Spring JdbcTemplate First import spring jdbc library: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> Second, create new java class for JdbcTemplate configuration like this: @Component public class JdbcConfig { @Value(“${spring.datasource.driverClassName}”) String… Read More »