<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Java Howto on developwith — developer tips</title>
    <link>https://developwith.com/categories/java-howto/</link>
    <description>Recent content in Java Howto on developwith — developer tips</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Fri, 12 Apr 2019 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://developwith.com/categories/java-howto/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>How to generate test numbers in Java</title>
      <link>https://developwith.com/how-do-i/java/generate-test-phonenumber/</link>
      <pubDate>Fri, 12 Apr 2019 00:00:00 +0000</pubDate>
      <guid>https://developwith.com/how-do-i/java/generate-test-phonenumber/</guid>
      <description>&lt;p&gt;To help with the number generation we can leverage &lt;a href=&#34;https://github.com/googlei18n/libphonenumber?utm_source=www.developwith.com&#34;&gt;Googles Phone Library&lt;/a&gt;. The library is primarily used for parsing numbers and validating them. But they do have a test utility that generates an example number. This example number unfortunately is only one number and can&amp;rsquo;t be used in a bunch of random tests.&lt;/p&gt;&#xA;&lt;h2 id=&#34;get-library&#34;&gt;Get library&lt;/h2&gt;&#xA;&lt;p&gt;Resolve the dependency through maven by defining it in your &lt;code&gt;pom.xml&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-xml&#34; data-lang=&#34;xml&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;nt&#34;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;com.googlecode.libphonenumber&lt;span class=&#34;nt&#34;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;nt&#34;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;libphonenumber&lt;span class=&#34;nt&#34;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;nt&#34;&gt;&amp;lt;version&amp;gt;&lt;/span&gt;8.10.9&lt;span class=&#34;nt&#34;&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#xA;or load via gradle:&lt;/p&gt;</description>
    </item>
    <item>
      <title>How to use certs with Java keystore</title>
      <link>https://developwith.com/how-do-i/java/use-certs-with-java-keystore/</link>
      <pubDate>Fri, 12 Apr 2019 00:00:00 +0000</pubDate>
      <guid>https://developwith.com/how-do-i/java/use-certs-with-java-keystore/</guid>
      <description>&lt;p&gt;When you install the Java JDK or JRE it comes with a utility called &lt;code&gt;keytool&lt;/code&gt; which manages the certificates and keys that the JVM can see and use. Using certs in java can be a hassle, but once you know some of the basic commands to explore truststores and keystores with &lt;code&gt;keytool&lt;/code&gt; it makes it that much easier.&lt;/p&gt;&#xA;&lt;p&gt;If you want to see the details of a stand alone cert:&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-sh&#34; data-lang=&#34;sh&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;keytool -printcert -v -file mydomain.crt&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>Java</title>
      <link>https://developwith.com/how-do-i/java/</link>
      <pubDate>Fri, 12 Apr 2019 00:00:00 +0000</pubDate>
      <guid>https://developwith.com/how-do-i/java/</guid>
      <description>&lt;p&gt;Practical answers for building with Java and Spring.&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;How do I disable Redis in Spring Boot?&lt;/strong&gt; — Exclude Redis auto-configuration when you don&amp;rsquo;t need it. &lt;a href=&#34;disable-redis-spring/&#34;&gt;→ Read more&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;How do I generate a test phone number?&lt;/strong&gt; — Create realistic phone numbers for test fixtures with Google Phone Utils. &lt;a href=&#34;generate-test-phonenumber/&#34;&gt;→ Read more&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;How do I use JPA for IN queries?&lt;/strong&gt; — Query a database table with an IN statement in JPA. &lt;a href=&#34;jpa-in-sql-query/&#34;&gt;→ Read more&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;How do I read a file into a String?&lt;/strong&gt; — Load classpath and filesystem resources using Apache Commons IO. &lt;a href=&#34;make-string-from-file/&#34;&gt;→ Read more&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;How do I handle rounding in Java?&lt;/strong&gt; — BigDecimal, Math.round, and precision control for number handling. &lt;a href=&#34;rounding-numbers-math/&#34;&gt;→ Read more&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;How do I use simple string templates?&lt;/strong&gt; — String formatting and template options beyond concatenation using Apache Commons Lang. &lt;a href=&#34;simple-string-template/&#34;&gt;→ Read more&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;How do I autowire beans in Spring?&lt;/strong&gt; — Constructor injection, qualifier resolution, and common pitfalls. &lt;a href=&#34;spring-autowiring-tips/&#34;&gt;→ Read more&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;How do I use certificates with a Java keystore?&lt;/strong&gt; — Import, manage, and reference SSL certificates in JVM keystores. &lt;a href=&#34;use-certs-with-java-keystore/&#34;&gt;→ Read more&lt;/a&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;</description>
    </item>
    <item>
      <title>Gotchas with numbers in Java</title>
      <link>https://developwith.com/how-do-i/java/rounding-numbers-math/</link>
      <pubDate>Thu, 11 Apr 2019 00:00:00 +0000</pubDate>
      <guid>https://developwith.com/how-do-i/java/rounding-numbers-math/</guid>
      <description>&lt;p&gt;When using numbers in Java, if you don&amp;rsquo;t explicit tell the compiler what type of number it expects it treats most numbers as integers. This becomes a problem with rounding and doing math on numbers to get the right accuracy in the calculations.&lt;/p&gt;&#xA;&lt;p&gt;For instance, if you are rounding a number:&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-java&#34; data-lang=&#34;java&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;Math&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;na&#34;&gt;round&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;1525443794&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&#xA;&lt;p&gt;The above is not equal to the below when its explicit with a &lt;code&gt;d&lt;/code&gt;:&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-java&#34; data-lang=&#34;java&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;Math&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;na&#34;&gt;round&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;1525443794d&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&#xA;&lt;p&gt;The reason this is different is because its trying to round an &lt;code&gt;int&lt;/code&gt; that is already rounded. Its related to when Java converted from 32 to 64 bits and how the number management is done now.  The &lt;code&gt;d&lt;/code&gt; works with an explicit int, but you&amp;rsquo;ll need to cast the number if its a variable.&lt;/p&gt;</description>
    </item>
    <item>
      <title>How to change JPA Id Generator</title>
      <link>https://developwith.com/how-do-i/java/hibernate-jpa-sequence-generator/</link>
      <pubDate>Wed, 10 Apr 2019 00:00:00 +0000</pubDate>
      <guid>https://developwith.com/how-do-i/java/hibernate-jpa-sequence-generator/</guid>
      <description>&lt;p&gt;When working with JPA and Hibernate, you&amp;rsquo;ll often need to control how primary key IDs are generated. By default, Hibernate uses an auto-increment strategy that varies across databases. The &lt;code&gt;@GeneratedValue&lt;/code&gt; annotation together with &lt;code&gt;@SequenceGenerator&lt;/code&gt; gives you fine-grained control over ID generation.&lt;/p&gt;&#xA;&lt;h2 id=&#34;the-problem&#34;&gt;The Problem&lt;/h2&gt;&#xA;&lt;p&gt;If you just use &lt;code&gt;@GeneratedValue(strategy = GenerationType.AUTO)&lt;/code&gt;, Hibernate will pick a strategy depending on the database dialect. This can lead to:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;A separate sequence table (&lt;code&gt;hibernate_sequence&lt;/code&gt;) being created unexpectedly&lt;/li&gt;&#xA;&lt;li&gt;ID values that don&amp;rsquo;t match what you expect&lt;/li&gt;&#xA;&lt;li&gt;Portability issues between dev (H2) and production (PostgreSQL/Oracle)&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;using-sequence-generator&#34;&gt;Using Sequence Generator&lt;/h2&gt;&#xA;&lt;p&gt;A &lt;code&gt;@SequenceGenerator&lt;/code&gt; lets you define a database sequence with a specific name, allocation size, and initial value:&lt;/p&gt;</description>
    </item>
    <item>
      <title>How to disable Redis loading in Spring boot</title>
      <link>https://developwith.com/how-do-i/java/disable-redis-spring/</link>
      <pubDate>Mon, 08 Apr 2019 00:00:00 +0000</pubDate>
      <guid>https://developwith.com/how-do-i/java/disable-redis-spring/</guid>
      <description>&lt;p&gt;When it comes to deploying a Spring boot application into production, you need to have some adaptability on startup with some of the services. Spring boot has a tendency to try and connect to a bunch of different services at once. One of which is redis.&lt;/p&gt;&#xA;&lt;p&gt;If you want to disable the auto connection for redis, add the following line to your application definition.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-json&#34; data-lang=&#34;json&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;err&#34;&gt;@EnableAutoConfiguration(exclude&lt;/span&gt; &lt;span class=&#34;err&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;err&#34;&gt;RedisAutoConfiguration.class)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#xA;&lt;p&gt;This will remove the auto configuration of redis allowing you to control the connection timing on your own.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Spring Autowiring Tips</title>
      <link>https://developwith.com/how-do-i/java/spring-autowiring-tips/</link>
      <pubDate>Sun, 07 Apr 2019 00:00:00 +0000</pubDate>
      <guid>https://developwith.com/how-do-i/java/spring-autowiring-tips/</guid>
      <description>&lt;p&gt;First, off when building out a Spring boot app you need to get your beans to load. In order to do this you need to setup a component scan. Sometimes &lt;code&gt;@Service&lt;/code&gt; objects don&amp;rsquo;t resolve properly, so you need to update the filters in the component scan. Also, the root of the application package is typically the default package unless you override the specific details. Overriding the component scan will allow you to change package scans for the given application.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Using JPA to make an IN Query</title>
      <link>https://developwith.com/how-do-i/java/jpa-in-sql-query/</link>
      <pubDate>Tue, 02 Apr 2019 00:00:00 +0000</pubDate>
      <guid>https://developwith.com/how-do-i/java/jpa-in-sql-query/</guid>
      <description>&lt;p&gt;There comes a time in using JPA with your database that you need to do a query with a list of values for a given field. In order to do this you&amp;rsquo;ll need to define the &lt;code&gt;IN&lt;/code&gt; query as a JPA query. With the &lt;code&gt;@Query&lt;/code&gt; annotation, define the column you need to query with the list of parameters. Reference the parameters with the &lt;code&gt;:parametername&lt;/code&gt; syntax after the &lt;code&gt;IN&lt;/code&gt; keyword.&lt;/p&gt;&#xA;&lt;p&gt;For a full example see:&lt;/p&gt;</description>
    </item>
    <item>
      <title>How to load a file into a String</title>
      <link>https://developwith.com/how-do-i/java/make-string-from-file/</link>
      <pubDate>Mon, 25 Mar 2019 12:18:54 -0400</pubDate>
      <guid>https://developwith.com/how-do-i/java/make-string-from-file/</guid>
      <description>&lt;p&gt;&lt;a href=&#34;https://commons.apache.org/proper/commons-io/?utm_source=www.developwith.com&#34;&gt;Apache Commons IO&lt;/a&gt; library has a lot of convient IO functionality that makes your life easier when dealing with files and input streams. One of these features is copying streams. This can be extremely helpful when you have to load a file in as a String. Java already has a lot of ways to write a String or treating String(s) as streams. Using the standard library and the &lt;a href=&#34;https://commons.apache.org/proper/commons-io/?utm_source=www.developwith.com&#34;&gt;Apache Commons IO&lt;/a&gt; library together you can do quite a bit.&lt;/p&gt;</description>
    </item>
    <item>
      <title>How to use a simple string template in java</title>
      <link>https://developwith.com/how-do-i/java/simple-string-template/</link>
      <pubDate>Mon, 25 Mar 2019 12:18:54 -0400</pubDate>
      <guid>https://developwith.com/how-do-i/java/simple-string-template/</guid>
      <description>&lt;p&gt;The simpliest way to have a string template in java is by using the &lt;a href=&#34;https://commons.apache.org/proper/commons-lang/?utm_source=www.developwith.com&#34;&gt;Apache Commons Lang library&lt;/a&gt; which you already have as a dependency already.&lt;/p&gt;&#xA;&lt;h2 id=&#34;get-library&#34;&gt;Get library&lt;/h2&gt;&#xA;&lt;p&gt;Resolve the dependency through maven by defining it in your &lt;code&gt;pom.xml&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-xml&#34; data-lang=&#34;xml&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nt&#34;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.apache.commons&lt;span class=&#34;nt&#34;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nt&#34;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;commons-lang3&lt;span class=&#34;nt&#34;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nt&#34;&gt;&amp;lt;version&amp;gt;&lt;/span&gt;3.8.1&lt;span class=&#34;nt&#34;&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nt&#34;&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#xA;&lt;h2 id=&#34;string-template-usage&#34;&gt;String template usage&lt;/h2&gt;&#xA;&lt;p&gt;To use the string template approach from &lt;a href=&#34;https://commons.apache.org/proper/commons-lang/?utm_source=www.developwith.com&#34;&gt;Apache Commons Lang library&lt;/a&gt;, you first define your data with a &lt;code&gt;Map&lt;/code&gt;. Then, you develop your template via a simple string or load it from a file. The template format is very similar to Kotlin String Templates, Velocity or Freemarker. The values are defined with the syntax &lt;code&gt;${map_key}&lt;/code&gt;.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
