Posts

Showing posts from September, 2019

OWASP Web Top 10 2017

Image
OWASP Web Top 10 2017 and what has changed compare to 2010? Go to link:  https://www.owasp.org/images/7/72/OWASP_Top_10-2017_%28en%29.pdf.pdf Source: www.owasp.org

REST Endpoint: Consume / Produce JSON/XML

Image
Import Jackson Data format: Annotate the REST method to consume and produce the desired format(s): In HTTP Request header, set attribute: Content-Type  to application/json|xml to indicate the body format Accept  to application/json|xml to indicate the desired response format

Java 8

https://howtodoinjava.com/java-8-tutorial/

Java Collections

https://howtodoinjava.com/java-collections/ https://howtodoinjava.com/interview-questions/useful-java-collection-interview-questions/

extends Thread vs implements Runnable

Thread   Class T1 extends Thread {     public void run(){...}   } Run with:   new T1().start(); Runnable   Class R2 implements Runnable{     public void run(){...}   } Run with:   Thread t2 = new Thread(R2);   t2.start(); R1 still allows you to extend a class (and thus, the behaviour), which is not possible with T1. Further, multiple threads of T2 shares the same runnable instance, where multiple T1 creating unique instances. If you don't need extention behaviour, use rather Runnable implementation.

Object mapping performance comparison

A performance comparison between: Dozer: recursively copies data from one object to another Orika: recursively copies (byte) data from one object to another MapStruct: Conde generator ModelMapper: simple, based on conventions JMapper: The framework allows for different ways of configuration: annotation-based, XML or API-based. See complete results here:  https://www.baeldung.com/java-performance-mapping-frameworks

CountDownLatch

The main thread initializes a CountDownLatch, e.g.:     CountDownLatch countDownLatch = new CountDownLatch(3); It passes countDownLatch to n (in this example 3) sub-threads, for instance via constructor. Finally, the main thread waits for the execution of the sub threads:     countDownLatch.await(); Once ready, every thread finally calls     countDownLatch.countDown(); Every countDown() call decrements the initial number, and the main thread finally will resume with the operations defined after the wait() statement. See more here: https://howtodoinjava.com/java/multi-threading/when-to-use-countdownlatch-java-concurrency-example-tutorial/

Spring 5 WebFlux: WebClient and WebTestClient

WebClient is part of Spring 5’s reactive web framework called Spring WebFlux... webClient   .post()     .uri("/uri")   .header(...)   .retrieve().bodyToFlux(Clazz.class);   Or    .retrieve().bodyToMono(Clazz.class); Go to: https://www.callicoder.com/spring-5-reactive-webclient-webtestclient-examples/

Unit testing Spring Boot applications

Check here a good overview of unit testing your spring boot application: 1- Unit Testing with Spring Boot 2- Testing Spring MVC Web Controllers with @WebMvcTest 3- Testing JPA Queries with @DataJpaTest 4- Integration Tests with @SpringBootTest Start with:  https://reflectoring.io/unit-testing-spring-boot/

Back to basics: Creating Custom Annotations

How to create custom annotations, and how to take advantage of them by using Java's Reflection API: https://www.baeldung.com/java-custom-annotation

@RequestBody @ResponseBody @RestController & ResponseEntity

@RequestBody Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object... @ResponseBody The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object... @RestController RestController-annotated controllers include @Controller and @ResponseBody by default . See the original article here:  https://www.baeldung.com/spring-request-response-body ResponseEntity   "... represents the whole HTTP response: status code, headers, and body. Because of it, we can use it to fully configure the HTTP response." https://www.baeldung.com/spring-response-entity

Post data using curl command

Simple example of posting data using curl command: curl -i \ -H "Accept: application/json" \ -H "Content-Type:application/json" \ -X POST --data    '{"param1": "val1", "param2": "val2"}' "https://localhost:8080/endpoint"

Lombok Features

Good overview of Lombok features: https://dzone.com/articles/introduction-to-lombok