随笔分类 - Java
摘要:Assumption: Run the testing code only if when the condition is match, otherwise, test will be ignored @Test void runTestIf() { System.out.print("Curre
阅读全文
摘要:Basic Query public interface BookRepository extends CrudRepository<Book, Long> { @Query("SELECT b from Book b WHERE b.available = true") List<Book> fi
阅读全文
摘要:Entity: Employee.java package com.skillsoft.springdatajpa.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import java
阅读全文
摘要:Stream<Employee> emps = ... TreeSet<Employee> tree = emps.collect( Collectors.toCollection( () -> new TreeSet<Employee>( Comparator.comparingInt(Emplo
阅读全文
摘要:To void: forEach, forEachOrdered, peek stream.peek(System.out::println) // print without termination .filter(n -> n > 0) .distinct() .limit(10) .forEa
阅读全文
摘要:Sream<Employee> emps = ...; emps.sorted( Comparator.comparingInt(Employee::getSalary) .reversed() ).limit(10) .map(Employee::getName) .forEachOrdered(
阅读全文
摘要:Why need to use @ResponseEntity, first let's see what problem we might have when we don't use it. @RestController public class ProductsRestController
阅读全文
摘要:Interceptor jump in before controller. So request may not reach controller if Pre-process is not ok. Demo: package com.test.hplus.interceptors; import
阅读全文
摘要:@ControllerAdvice public class DefaultAttributeController { // apply default value @ModelAttribute("newuser") public User getDefaultUser() { return ne
阅读全文
摘要:Create file: // this apply to all the controllers @ControllerAdvice public class ApplicationExceptionHandler { @ExceptionHandler(ApplicationException.
阅读全文
摘要:Controller: @Controller public class LoginController { private UserRepository userRepository; @PostMapping("/login") public String login(@ModelAttribu
阅读全文
摘要:If you need to convert a deep nested object struture, you cannot just use @InitBinder, you need to build a custom convertor: Exp: conver city prop in
阅读全文
摘要:For example, from the client, it send date as string to BE. But BE requires date to be a Date instead of String. Controller: .. @Autowired private Use
阅读全文
摘要:User entity: import javax.validation.constraints.*; @Entity public class User { @Id private int id; @Size(min = 6, message = "Username cannot be less
阅读全文
摘要:@Repository public interface ProductRepository extends CrudRepository<Product, Integer> { @Query("select p from Product p where p.name like %:name%")
阅读全文
摘要:Set<Book> lib = ... lib.stream() .flatMap(book -> book.getWords().stream()) .distinct() .forEach(System.out::println);
阅读全文
摘要:Entity: package com.frankmoley.security.app.auth; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Generated
阅读全文
摘要:Code to improve: final Collection<DiscussionTopics> topics = s.getTopics(); final boolean anyTopics = topics != null && !topics.isEmpty(); Change to:
阅读全文
摘要:Module defined what can be used inside the module package and what outside world can use inside our module. module-info.java /* * To change this licen
阅读全文
摘要:public class Code { public static void main(String[] args) { IntStreams.range(1,4) .forEach(System.out::println); // 1 2 3 Arrays.stream(new int[] {1,
阅读全文