lundi 18 janvier 2021

How to Handle Java 8 Dates and Time with Jackson in Spring Boot (JSR-310)

When you use Spring Boot, you can add the following line to the application.properties file:

spring.jackson.serialization.write-dates-as-timestamps=false

Spring Boot enables the configuration of the default ObjectMapper with properties under the spring.jackson prefix. Read the jackson section in the Spring Boot docs for more info on this.

more details :

https://www.baeldung.com/spring-boot-customize-jackson-objectmapper

https://codeboje.de/jackson-java-8-datetime-handling/

How to Trace Transaction Behavior Details in Spring Boot

To log transaction details in a Spring Boot application simply add the following settings in application.properties:

 logging.level.org.springframework.transaction.interceptor=TRACE

mardi 12 janvier 2021

Docker : Dockerfile COPY vs ADD, which one to use?

From Docker Doc : 

ADD or COPY

Although ADD and COPY are functionally similar, generally speaking, COPY is preferred. That’s because it’s more transparent than ADDCOPY only supports the basic copying of local files into the container, while ADD has some features (like local-only tar extraction and remote URL support) that are not immediately obvious. Consequently, the best use for ADD is local tar file auto-extraction into the image, as in ADD rootfs.tar.xz /.

If you have multiple Dockerfile steps that use different files from your context, COPY them individually, rather than all at once. This ensures that each step’s build cache is only invalidated (forcing the step to be re-run) if the specifically required files change.

For example:

COPY requirements.txt /tmp/
RUN pip install --requirement /tmp/requirements.txt
COPY . /tmp/

Results in fewer cache invalidations for the RUN step, than if you put the COPY . /tmp/ before it.

Because image size matters, using ADD to fetch packages from remote URLs is strongly discouraged; you should use curl or wget instead. That way you can delete the files you no longer need after they’ve been extracted and you don’t have to add another layer in your image. For example, you should avoid doing things like:

ADD https://example.com/big.tar.xz /usr/src/things/
RUN tar -xJf /usr/src/things/big.tar.xz -C /usr/src/things
RUN make -C /usr/src/things all

And instead, do something like:

RUN mkdir -p /usr/src/things \
    && curl -SL https://example.com/big.tar.xz \
    | tar -xJC /usr/src/things \
    && make -C /usr/src/things all

For other items (files, directories) that do not require ADD’s tar auto-extraction capability, you should always use COPY.

Source : https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

Only the instructions RUNCOPYADD create layers. Other instructions create temporary intermediate images, and do not increase the size of the build.

mardi 5 janvier 2021

Spring Security : How to get the current logged in user object from spring security?

 Since version 5.2 you can use CurrentSecurityContext annotation to get the current user authentication:

@GetMapping("/authentication")
public Object authentication(@CurrentSecurityContext(expression="authentication")
                             Authentication authentication) {
    return authentication.getDetails();
}

or even:

@GetMapping("/hello")
public String hello(@CurrentSecurityContext(expression="authentication.name")
                    String username) {
    return "Hello, " + username + "!";
}
https://stackoverflow.com/questions/32052076/how-to-get-the-current-logged-in-user-object-from-spring-security