Cloud-oriented Life

Cloud Native Technology Improves Lives

EasyExcel

EasyExcel rewrote Apache POI’s analysis of version 07 of Excel. A 3M excel using POI sax analysis still requires about 100M of memory. Switching to easyexcel can be reduced to a few M, and no amount of memory overflow will occur in the larger excel; version 03 depends on The sax mode of POI is encapsulated in the upper layer for model conversion, which makes users more simple and convenient.

Read more »

Automatic restart Spring Boot application

With the spring-boot-devtools module, your application will restart every time files on the classpath change. If IntelliJ IDEA is configured to continuously compile changed files, you can set a trigger file. In this case your application will restart only after you modify the trigger file. For more information, see Automatic Restart Developing with Spring Boot - https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.devtools.restart.

Read more »

Spring Boot Test

FAQs

No qualifying bean of type ‘org.springframework.boot.test.web.client.TestRestTemplate’ available

1
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.test.web.client.TestRestTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

TestRestTemplate is only auto-configured when @SpringBootTest has been configured with a webEnvironment that means it starts the web container and listens for HTTP requests. For example:

Note the use of webEnvironment=RANDOM_PORT to start the server with a random port (useful to avoid conflicts in test environments)

1
2
3
4
5
6
7
8
9
10
11
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoServiceTest {

@Autowired
private DemoService demoService;

@Test
public void testCreate() {
demoService.Create();
}
}

References

[1] Getting Started | Testing the Web Layer - https://spring.io/guides/gs/testing-web/

Spring Boot Validation

Usages

UUID Validataion

Define a UUID Validataion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.constraints.Pattern;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Pattern(regexp="^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$", message = "Not a valid UUIDPattern")
@Retention(RUNTIME)
@Target({FIELD, METHOD, PARAMETER})
@Constraint(validatedBy = {})
@Documented
public @interface UUIDPattern {
String message() default "{invalid.uuid}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

Example

1
2
3
4
5
6
public class User {
@Id
@GeneratedValue
@UUIDPattern
protected UUID id;
}

java - Is there a uuid validator annotation? - Stack Overflow - https://stackoverflow.com/questions/37320870/is-there-a-uuid-validator-annotation

Java validator for UUIDs - https://gist.github.com/mindhaq/ffc3378a271cb7e91b3819ff1abb675c

FAQs

HV000030: No validator could be found for constraint ‘javax.validation.constraints.Pattern’ validating type ‘java.util.UUID’.

As stated in problem, to solve this error you MUST use correct annotations. In above problem, @Pattern annotation must be applied on any String field only.

TODO.

References

Validation with Spring Boot - the Complete Guide - Reflectoring - https://reflectoring.io/bean-validation-with-spring-boot/

[1] Validation in Spring Boot | Baeldung - https://www.baeldung.com/spring-boot-bean-validation

asdf and Golang (Go) Plugin

asdf is a single CLI tool for managing multiple runtime versions. It extend with a simple plugin system to install your favourite language: Dart, Elixir, Flutter, Golang (Go), Java, Node.js, Python, Ruby …

This article is about how to use asdf and Golang (Go) plugin to install multiple Golang (Go) versions on macOS with the Homebrew package manager.

Read more »

asdf and Java Plugin

asdf is a single CLI tool for managing multiple runtime versions. It extend with a simple plugin system to install your favourite language: Dart, Elixir, Flutter, Golang (Go), Java, Node.js, Python, Ruby …

This article is about how to use asdf and Java plugin to install multiple Java versions on macOS with the Homebrew package manager.

Read more »

Logging SQL with p6spy

P6Spy is a framework that enables database data to be seamlessly intercepted and logged with no code changes to existing application. The P6Spy distribution includes P6Log, an application which logs all JDBC transactions for any Java application.

Spring Boot autoconfiguration is handled by the separate project: gavlyukovskiy/spring-boot-data-source-decorator: Spring Boot integration with p6spy, datasource-proxy, flexy-pool and spring-cloud-sleuth - https://github.com/gavlyukovskiy/spring-boot-data-source-decorator, please consult the respective documentation for usage.

Read more »

Logging Java Persistence API (JPA) and Hibernate SQL

Spring JDBC and JPA provide abstractions over native JDBC APIs, allowing developers to do away with native SQL queries. However, we often need to see those auto-generated SQL queries and the order in which they were executed for debugging purposes.

Read more »
0%