{"id":208,"date":"2024-01-15T19:41:54","date_gmt":"2024-01-15T18:41:54","guid":{"rendered":"https:\/\/fixthebugs.com\/?p=208"},"modified":"2024-01-15T19:41:55","modified_gmt":"2024-01-15T18:41:55","slug":"spring-allow-null-in-value","status":"publish","type":"post","link":"https:\/\/fixthebugs.com\/index.php\/2024\/01\/15\/spring-allow-null-in-value\/","title":{"rendered":"Spring: Allow null in @Value"},"content":{"rendered":"\n<p><strong>Problem<\/strong>:  To allow null in @value or to set null as default in Spring @value annotation if the property does not exist.<\/p>\n\n\n\n<p><strong>Solution<\/strong>: after property include <code>:#{null}<\/code><\/p>\n\n\n\n<p><strong>Example<\/strong>: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Non null allowed value\n@Value(\"${property.value}\")\nString propertyName\n\n\/\/ <strong>Nullable and null as default value<\/strong>\n@Value(\"${property.value<span style=\"background-color: transparent; font-family: inherit; font-size: inherit; font-weight: inherit;\"><strong>:#{null}<\/strong><\/span>}\")\nString propertyName<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>: Here we are using Spring Expression Language (SpEL) to set null as default value if does not exist in properties.<\/p>\n\n\n\n<p>Source: <x> <a rel=\"dofollow noopener\" href=\"https:\/\/docs.spring.io\/spring-framework\/docs\/3.0.x\/reference\/expressions.html\" target=\"_blank\">documentation<\/a><\/x><\/p>\n\n\n\n<p>For more interesting tutorials &amp; guides just check them&nbsp;<a rel=\"dofollow\" href=\"https:\/\/fixthebugs.com\/index.php\/category\/tutorials-guides\/\">HERE<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Allow null in @Value<\/p>\n","protected":false},"author":1,"featured_media":210,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[9,11],"tags":[52,48,49,51,50],"_links":{"self":[{"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/posts\/208"}],"collection":[{"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/comments?post=208"}],"version-history":[{"count":3,"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":212,"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/posts\/208\/revisions\/212"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/media\/210"}],"wp:attachment":[{"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/media?parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/categories?post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fixthebugs.com\/index.php\/wp-json\/wp\/v2\/tags?post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}