Disabling Spring Security

https://stackoverflow.com/questions/47273691/spring-boot-2-0-disable-default-security

November 2, 2022 · word · Peter Dieleman

Disabling CORS in local development

Problem Access to the XMLHttpRequest at ‘http://localhost:8084’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Frontend side Disabling CORS in https://stackoverflow.com/questions/46337471/how-to-allow-cors-in-react-js https://nabil6391.medium.com/avoid-cors-requests-for-a-react-app-2988e0061c1a https://reactjs.org/docs/cross-origin-errors.html <> Backend side https://howtodoinjava.com/spring-boot2/spring-cors-configuration/ Should we be doing this in prod? https://zetcode.com/springboot/cors/ https://www.baeldung.com/spring-cors https://www.baeldung.com/spring-security-cors-preflight If we use Spring Security in our project, we must take an extra step to make sure it plays well with CORS....

August 2, 2022 · 105 words · Peter Dieleman

Covering Cornering Cases

Defining Unique constraints over multiple columns https://www.baeldung.com/jpa-unique-constraints https://stackoverflow.com/questions/3126769/uniqueconstraint-annotation-in-java

July 29, 2022 · 8 words · Peter Dieleman

Best Practice Ignoring Fields from a PUT payload

Sources https://stackoverflow.com/questions/51680343/ignore-fields-from-specific-request-in-spring-boot https://stackoverflow.com/questions/12505141/only-using-jsonignore-during-serialization-but-not-deserialization https://www.concretepage.com/jackson-api/jackson-jsonignore-jsonignoreproperties-and-jsonignoretype

May 16, 2022 · 4 words · Peter Dieleman

Jackson exclude null values

Sources https://stackoverflow.com/questions/64868946/how-to-not-return-null-value-in-responseentity https://www.baeldung.com/jackson-ignore-null-fields https://stackoverflow.com/questions/11757487/how-to-tell-jackson-to-ignore-a-field-during-serialization-if-its-value-is-null Issue We have the following setting in the application.yml file, but it is ignored: spring: jackson: default-property-inclusion: non_null Manually annotating the classes with @JsonInclude(JsonInclude.Include.NON_NULL) did reinstate the wanted behaviour. However, this is painful for classes that are pulled from libraries. Root cause seems to be related to the following: https://stackoverflow.com/questions/62819472/spring-jackson-default-property-inclusion-ignored. ObjectMapper Bean in our config. Add the following: .serializationInclusion(JsonInclude.Include.NON_NULL).

May 16, 2022 · 63 words · Peter Dieleman

Feign Client converting GET to POST

Sources https://stackoverflow.com/questions/53340164/feignclient-converts-get-method-to-post https://stackoverflow.com/questions/53340164/feignclient-converts-get-method-to-post https://stackoverflow.com/questions/58468968/feign-client-get-request-throws-method-not-allowed-request-method-post-not Presence of requestbody automatically converts it to a POST

May 12, 2022 · 13 words · Peter Dieleman

Spring Validation Context

Sources https://stackoverflow.com/questions/69828371/how-to-handle-multiple-combination-of-java-bean-validation-in-spring https://www.baeldung.com/spring-mvc-custom-validator#custom-class-level-validation https://stackoverflow.com/questions/19089649/best-practices-for-validation-depending-on-actions-spring-mvc https://www.baeldung.com/javax-validation-groups https://reflectoring.io/bean-validation-with-spring-boot/#using-validation-groups-to-validate-objects-differently-for-different-use-cases

May 5, 2022 · 6 words · Peter Dieleman

Db Locking

https://dzone.com/articles/concurrency-and-locking-with-jpa-everything-you-ne https://www.baeldung.com/jpa-pessimistic-locking https://blog.arnoldgalovics.com/jpa-optimistic-locking/ It’s also possible to lock multiple rows at the same time, this can be done by executing a custom select query and setting the lock mode. Exclusive lock table https://stackoverflow.com/questions/64224014/include-additional-columns-in-where-clause-of-hibernate-jpa-generated-update-que https://stackoverflow.com/questions/65049057/spring-data-jpa-insert-into-multiple-tables-to-avoid-locking-tables @Query(value = "LOCK TABLE SKU IN EXCLUSIVE MODE", nativeQuery = true) @Modifying void lockTable();

April 23, 2022 · 48 words · Peter Dieleman

Customize error message Spring Boot

Sources https://stackoverflow.com/questions/62561211/spring-responsestatusexception-does-not-return-reason https://www.baeldung.com/global-error-handler-in-a-spring-rest-api https://www.amitph.com/spring-rest-api-custom-error-messages/ https://auth0.com/blog/get-started-with-custom-error-handling-in-spring-boot-java/ https://stackoverflow.com/questions/45317638/how-to-catch-accessdeniedexception-in-spring-boot-rest-api https://stackoverflow.com/questions/59302621/custom-message-in-spring-accessdeniedexception https://dzone.com/articles/best-practice-for-exception-handling-in-spring-boo @ControllerAdvice annotation Note AccessDeniedException needs to be taken care of in filter chain

April 12, 2022 · 21 words · Peter Dieleman

Switching Spring Boot Application.yml properties from IntelliJ

Use: --spring.config.name=myproject, where myproject is the name of the *.yml or *.properties file that is stored under the default /config directory. The extension is not required. https://docs.spring.io/spring-boot/docs/2.1.13.RELEASE/reference/html/boot-features-external-config.html Further Documentation https://www.baeldung.com/spring-yaml https://baeldung-cn.com/spring-yaml-vs-properties https://docs.spring.io/spring-boot/docs/1.0.1.RELEASE/reference/html/howto-properties-and-configuration.html https://docs.spring.io/spring-boot/docs/1.0.1.RELEASE/reference/html/howto-properties-and-configuration.html#howto-change-configuration-depending-on-the-environment need: spring.config.activate.on-profile: “profile_name” example: spring: # default config without profile name . . . --- spring: config: activate: on-profile: "profile name" In conjunction with: ./gradlew run --args='--spring.profiles.active=profile_name' Can make this even more complicated when activating multiple profiles at once with overlapping properties, in that case the precedence order of profiles needs to be defined....

March 28, 2022 · 107 words · Peter Dieleman

Setting a default format for Java 8 Date types

https://www.baeldung.com/spring-boot-formatting-json-dates https://stackoverflow.com/questions/66694406/spring-boot-rest-offsetdatetime-returned-as-float

March 24, 2022 · 2 words · Peter Dieleman

Debugging Spring Security AAD

Disabling Spring Security https://github.com/Azure/azure-sdk-for-java/pull/12645/files

March 23, 2022 · 4 words · Peter Dieleman

Hibernate Envers

Blog Posts / Documentation https://docs.jboss.org/envers/docs/ https://vladmihalcea.com/the-best-way-to-implement-an-audit-log-using-hibernate-envers/ https://thorben-janssen.com/hibernate-envers-getting-started/ https://sunitc.dev/2020/01/21/spring-boot-how-to-add-jpa-hibernate-envers-auditing/ https://www.bytefish.de/blog/hibernate_envers_versioning_and_auditing.html https://hibernate.atlassian.net/browse/HHH-10212?attachmentOrder=asc https://developer.jboss.org/thread/152642 https://developer.jboss.org/thread/166705 https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.4/html/developing_hibernate_applications/hibernate_envers Further Issues @CreatedDate, @CreatedBy, etc. Annotations https://www.baeldung.com/database-auditing-jpa https://rashidi.github.io/spring-boot-data-audit/ @CreatedDate, @CreatedBy, @LastModifiedDate, and @LastModifiedBy. createdBy and modifiedBy fields will be automatically populated if Spring Security is available in the project path. @CreatedDate & @LastModifiedDate https://stackoverflow.com/questions/49170180/createdby-and-lastmodifieddate-are-no-longer-working-with-zoneddatetime https://stackoverflow.com/questions/43236431/register-a-new-date-converter-auditable-in-spring-data-mongodb-for-zoneddatetime https://stackoverflow.com/questions/49170180/createdby-and-lastmodifieddate-are-no-longer-working-with-zoneddatetime java.lang.IllegalArgumentException: Invalid date type for member <MEMBER NAME>! Supported types are [org.joda.time.DateTime, org.joda.time.LocalDateTime, java.util.Date, java.lang.Long, long]. https://github.com/spring-projects/spring-data-commons/issues/880 Which finally contains this amazing quote:...

January 1, 2022 · 188 words · Peter Dieleman