Notes on Java Concurrency
Atomic Reference https://stackoverflow.com/questions/3964211/when-to-use-atomicreference-in-java https://jenkov.com/tutorials/java-util-concurrent/atomicreference.html
Atomic Reference https://stackoverflow.com/questions/3964211/when-to-use-atomicreference-in-java https://jenkov.com/tutorials/java-util-concurrent/atomicreference.html
Map inputString.map(x -> { if (!StringUtils.isBlank(x)) { return x; } else { return Optional.empty(); } }).orElse(Optional.empty()); Filter Java < 16 return thing.getAllSubEntities() .stream() .filter(e -> e.isDisabled()) .collect(Collectors.toList()); Java >= 16 return thing.getAllSubEntities() .stream() .filter(e -> e.isDisabled()) .tolist();
Sources https://dzone.com/articles/think-twice-before-using-reflection https://www.javacodegeeks.com/2015/09/how-to-use-reflection-effectively.html https://www.baeldung.com/java-reflection
Unpacking a List of results https://stackoverflow.com/questions/43850101/rest-assured-json-body-assertion-for-set-of-values-in-list-regardless-of-posit https://stackoverflow.com/questions/68794097/how-to-get-list-of-objects-with-restassured-instead-of-array-of-objects https://stackoverflow.com/questions/15531767/rest-assured-generic-list-deserialization https://stackoverflow.com/questions/49324680/how-to-verify-that-array-contains-object-with-rest-assured https://www.james-willett.com/rest-assured-extract-json-response/ https://devqa.io/parse-json-response-rest-assured/ List<Person> persons = given() .when() .get("/person") .then() .extract() .body() // here's the magic .jsonPath().getList(".", Person.class); Further Notes on Logging ALL asserts (even passing ones) How to output of Asserts (expected, actual) to console, for failing as well as succesfull tests? https://stackoverflow.com/questions/3963708/gradle-how-to-display-test-results-in-the-console-in-real-time https://github.com/radarsh/gradle-test-logger-plugin https://mkyong.com/gradle/gradle-display-test-results-in-console/ https://stackoverflow.com/questions/56436541/assertj-log-all-passing-and-failing-assertions https://stackoverflow.com/questions/56436541/assertj-log-all-passing-and-failing-assertions https://github.com/assertj/assertj/issues/1518 https://assertj.github.io/doc/#assertj-core-assertion-description-consumer https://stackoverflow.com/questions/65113051/junit5-assertj-create-log-entry-on-assertion https://stackoverflow.com/questions/28994316/can-you-add-a-custom-message-to-assertj-assertthat https://stackoverflow.com/questions/57334283/is-it-possible-to-log-the-name-and-values-of-fields-being-asserted-along-with-co https://stackoverflow.com/questions/15926005/how-make-junit-print-assertion-results AssertJ Alternatives (maybe these support it?) https://blog.frankel.ch/comparison-assertion-libraries/ Hamcrest Does not change the story....
Should we map both sides? https://stackoverflow.com/questions/30464782/how-to-maintain-bi-directional-relationships-with-spring-data-rest-and-jpa/30474303#30474303 It usually simplifies the matter if you try not to use bi-directional relationship whenever possible and rather fall back to a repository to obtain all the entities that make up the backside of the association. https://thorben-janssen.com/best-practices-many-one-one-many-associations-mappings/ https://twitter.com/odrotbohm/status/603247455094841344 https://stackoverflow.com/questions/48754783/do-i-have-to-set-both-sides-for-a-bidirectional-relationship If Yes - Do we have to manually update both sides? https://stackoverflow.com/questions/7546161/update-bidirectional-manytomany-from-both-sides https://stackoverflow.com/questions/20068742/jpa-updating-bidirectional-association https://hellokoding.com/jpa-one-to-many-relationship-mapping-example-with-spring-boot-maven-and-mysql/ CascadeType.ALL is for propagating the CRUD operations on the parent entity to the child entities....
https://stackoverflow.com/questions/25415738/how-to-persist-arraylist-within-spring-entity-class Need: @ElementCollection & @CollectionTable(name="abc") ? https://thorben-janssen.com/hibernate-tips-elementcollection/ Does this still require a separate table? https://stackoverflow.com/questions/26512916/what-column-type-is-required-when-using-elementcollection-with-jpa https://www.postgresql.org/docs/current/arrays.html https://sajithv.medium.com/spring-jpa-data-with-postgres-array-types-a6cc4be421e2 Hibernate & Arrays From: Vlad Mihalcea " Hibernate ORM does not support ARRAY column types" Also see: https://stackoverflow.com/questions/1647583/how-to-map-a-postgresql-array-with-hibernate Semi-canonical solution Use: https://github.com/vladmihalcea/hibernate-types https://vladmihalcea.com/how-to-map-java-and-sql-arrays-with-jpa-and-hibernate/ PostgreSQL data types https://www.postgresql.org/docs/current/arrays.html And there’s this https://stackoverflow.com/questions/69858533/replacement-for-hibernates-deprecated-type-annotation https://stackoverflow.com/questions/4332467/mapping-array-with-hibernate https://stackoverflow.com/questions/62647125/hibernate-envers-does-not-recognize-custom-types-from-hibernate-types-52
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....
Defining Unique constraints over multiple columns https://www.baeldung.com/jpa-unique-constraints https://stackoverflow.com/questions/3126769/uniqueconstraint-annotation-in-java
How to share config between developers? Q: Can we check in (parts of) the .idea folder as in VS Code? https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 All files under the .idea directory in the project root except the items that store user-specific settings: workspace.xml usage.statistics.xml shelf directory All the .iml module files (can be located in different module directories) -> applies to IntelliJ IDEA https://www.jetbrains.com/help/idea/creating-and-managing-projects.html#share-project-through-vcs SO says https://stackoverflow.com/questions/43198273/which-files-in-idea-folder-should-be-tracked-by-git?r=SearchResults&s=1%7C91.0115 Jetbrains official gitignore https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore But but I just want to set required plugins for the whole team https://www....
Long List of Query params / request parameters http://dolszewski.com/spring/how-to-bind-requestparam-to-object/ “You also need to mark the POJO parameter in controller’s method with the @Valid annotation. This way you inform Spring that it should execute validation on the binding step.” Downsides: are not able to map query param to a nice variable name in case the ones in URL are shortened (q instead of query), unless you specify a different getter. Note In many cases, it makes much more sense to use @NotBlack instead @NotNull as it also covers the undesired empty string problem (a string with the length of zero)....
Comparisons https://phauer.com/2018/moving-back-from-gradle-to-maven/ https://gradle.org/gradle-vs-maven-performance/ https://dzone.com/articles/gradle-vs-maven https://www.baeldung.com/ant-maven-gradle Further Gradle Info https://tomgregory.com/5-reasons-to-switch-to-the-gradle-kotlin-dsl/ https://www.oreilly.com/library/view/gradle-beyond-the/9781449373801/ch04.html Further Maven Info Essential Maven Plugins From https://javarevisited.blogspot.com/2016/08/top-10-maven-plugins-every-java-developer-know.html: maven-compiler-plugin: This is the most important maven plugin. You almost always use it unknowingly. This plugin compiles your Java code from the standard location Maven specifies e.g. /src/main/java and /src/main/resources. maven-surefire-plugin: The Maven Surefire plugin is the default plugin for running unit tests. You can also customize the behavior of this plugin by specifying the configuration in pom....
SourceCompatability vs. TargetCompatability https://stackoverflow.com/questions/16654951/gradle-sourcecompatibility-vs-targetcompatibility sourceCompatibility = JavaVersion.VERSION_11 Java Versions https://stackoverflow.com/questions/9170832/list-of-java-class-file-format-major-version-numbers gradle INVALID SOURCE RELEASE 17 https://stackoverflow.com/questions/70320448/spring-boot-gradle-build-invalid-source-release-11
Sources https://stackoverflow.com/questions/32884195/filter-values-only-if-not-null-using-lambda-in-java8
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
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).
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
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
Sources https://stackoverflow.com/questions/26684361/filter-child-object-in-spring-data-query https://stackoverflow.com/questions/42857947/jpa-with-specification-how-to-filter-an-entity-by-a-child-collection-content https://stackoverflow.com/questions/47867124/spring-data-jpa-specification-how-to-filter-a-parent-object-by-its-children-obj https://www.baeldung.com/jpa-and-or-criteria-predicates?fbclid=IwAR0vao7fFrB_NghXBEesu-cCFN9HS4WfPcmCXvTkKIvO72sIWu2Nz2aST4A https://stackoverflow.com/questions/55629512/how-to-filter-parent-object-which-has-list-of-children-and-a-child-has-list-of-g https://tousu.in/qa/?qa=873798/ https://stackoverflow.com/questions/25627484/how-to-filter-child-entities-collections-with-predicate?noredirect=1&lq=1 Fetch Joins https://stackoverflow.com/questions/71840199/how-to-fix-jpa-specification-api-with-join-on-condition-returns-2-query-instead More detail - Multiselect https://www.initgrep.com/posts/java/jpa/select-values-in-criteria-queries Subquery https://stackoverflow.com/questions/53678146/jpa-2-1-predicate-criteria-for-child-entity\ Matching against a list https://stackoverflow.com/questions/13940249/jpa-criteria-api-matching-against-a-list-in-spring-data-jpa-specifications Further Info https://stackoverflow.com/questions/47469861/what-is-the-difference-between-a-criteria-a-predicate-and-a-specification
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
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....
https://www.baeldung.com/spring-boot-formatting-json-dates https://stackoverflow.com/questions/66694406/spring-boot-rest-offsetdatetime-returned-as-float
Baeldung GameDTO contains only two fields, but the field types and names perfectly match the source. In such a case, ModelMapper handles the conversion without additional configuration: https://www.baeldung.com/java-modelmapper Use a converter, as outlined above.
Disabling Spring Security https://github.com/Azure/azure-sdk-for-java/pull/12645/files
Notes The SQL standard requires that writing just timestamp be equivalent to timestamp without time zone, and PostgreSQL honors that behavior. timestamptz is accepted as an abbreviation for timestamp with time zone; this is a PostgreSQL extension. Precision can be specified for time, timestamp, and interval types, and can range from 0 to 6. If no precision is specified in a constant specification, it defaults to the precision of the literal value (but not more than 6 digits)....
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:...
https://stackoverflow.com/questions/4486787/jackson-with-json-unrecognized-field-not-marked-as-ignorable @JsonIgnoreProperties(ignoreUnknown = true)