-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
113 lines (96 loc) · 21.2 KB
/
index.html
File metadata and controls
113 lines (96 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html> <html> <head> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-131515183-1"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-131515183-1'); </script> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Eder Luis Jorge</title> <meta name="description" content="Code lover! 20+ years of experience in IT area, and have done or collaborated with many important projects for the companies where I have worked with. I am a Java developer and a trainer of many IBM products official courses, and unofficial AWS courses, with a high rate from students, helping them to maintain their pace of learning through encouragement, active listening and assertive techniques that challenge them to keep pace with their professional goals. "> <link rel="stylesheet" href="/assets/css/main.css"> <link rel="canonical" href="http://localhost:4000/"> <link rel="alternate" type="application/rss+xml" title="Eder Luis Jorge" href="http://localhost:4000/feed.xml"> <script src="/assets/js/modernizr.js"></script> </head> <body"> <main class="wrapper"> <header class="site-header"> <a href="#navbar" id="menu-burger" class="menu-burger">Menu <span class="nav-icon"></span> <svg x="0px" y="0px" width="54px" height="54px" viewBox="0 0 54 54"> <circle fill="transparent" stroke="#000000" stroke-width="1" cx="27" cy="27" r="25" stroke-dasharray="157 157" stroke-dashoffset="157"></circle> </svg> </a> <div id="navbar" class="navbar"> <div class="navigation-wrapper"> <div class="half-block"> <h2>Navigation</h2> <nav> <ul class="primary-nav"> <li><a href="/">Home</a></li> <li><a href="/blog">Blog</a></li> <li><a href="/about">About</a></li> <li><a href="http://github.com/edinhojorge" target="_blank"><i class="icon icon-github"></i> Github</a></li> <li><a href="http://twitter.com/edinhojorge" target="_blank"><i class="icon icon-twitter"></i> Twitter</a></li> <li><a href="/feed.xml" target="_blank"><i class="icon icon-feed"></i> RSS</a></li> </ul> </nav> </div> </div> </div> </header> <article class="post"> <header class="post-header intro"> <div class="intro-in"> <h1 class="post-title">Eder Luis Jorge</h1> <h2 class="intro-subtitle">All that tech!</h2> </div> </header> <div class="post-content container"> <p>posted in Dec 29, 2018</p> <h1 id="what-about-summer">What about summer?</h1> <p>Well, I have been focused on Java EE for a long time, and with specific projects that were more focused on problems that were easily addressed by Java EE with base technologies like Java Server Faces for UI, EJB for business and JPA for persistence, and all related tools to build an app. Latelly, I was looking how the market is working with high class software around the world, and I’ve found that Spring framework is one of the most used currently. Companies take normally one decision, to use Java EE stack or to use Spring Framework stack.</p> <p>There are plenty of material around the internet, but I miss some basics to show for someone who doesn’t know about Spring, even for advanced topics, and then I decided to write a serie of articles to teach you how to build great apps with this wonderful framework.</p> <p>Starting now, I want to explain some basics for the <code class="highlighter-rouge">Spring Boot</code> and <code class="highlighter-rouge">Spring</code> itself. Spring is a framework focused on IoC pattern, or Inversion of Control. With IoC, you add a lot of flexibility in your software, build it faster, and when we use Spring, use less code. The Spring Framework is a big umbrella, just like Java EE, and there are many technologies under it. You will find persistence packages, REST packages, Web packages, log packages, social login packages, and many others. One big thing compared to Java EE is that Java EE normally defines the reference or gives something that take some considerable effort to build the full app, and on the other hand Spring has more flexibility and more complete packages and it will be much fast to build the same thing.</p> <p>Spring Boot is one of the components/packages of Spring stack. The beauty here is that it groups many projects of Spring Framework for a specific end. Let’s say you want to start a web app, there is a Spring Boot for that, that has Spring MVC, Rest, JPA, etc, pre-built. Spring Boot also identifies any library from Spring Framework and automatically initialises it. This is very good, because dependencies are solved with compatible libraries. Even better now is that you don’t even need to setup a web container. Building using spring boot application, your app is ready to run anywhere. Think about how can be easy to build your microservice using Spring Boot and deploy across many servers.</p> <p>Stop talking, let’s make some code here. Let’s use Spring Boot to build a web application, using maven for that.</p> <p>Basic dependency you are going to add to your <code class="highlighter-rouge">pom.xml</code> file is <code class="highlighter-rouge">org.springframework.boot</code>, with the <code class="highlighter-rouge">spring-boot-starter-test</code>. Check the full file for this project:</p> <figure class="highlight"><pre><code class="language-xml" data-lang="xml"><span class="nt"><project</span> <span class="na">xmlns=</span><span class="s">"http://maven.apache.org/POM/4.0.0"</span>
<span class="na">xmlns:xsi=</span><span class="s">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span class="na">xsi:schemaLocation=</span><span class="s">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span><span class="nt">></span>
<span class="nt"><modelVersion></span>4.0.0<span class="nt"></modelVersion></span>
<span class="nt"><groupId></span>com.github.edinhojorge.spring<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>springbootweb-starter<span class="nt"></artifactId></span>
<span class="nt"><version></span>0.0.1-SNAPSHOT<span class="nt"></version></span>
<span class="nt"><packaging></span>war<span class="nt"></packaging></span>
<span class="nt"><name></span>Spring boot web<span class="nt"></name></span>
<span class="nt"><description></span>A small spring boot app, showing the first steps for this framework<span class="nt"></description></span>
<span class="nt"><url></span>https://edinhojorge.github.com<span class="nt"></url></span>
<span class="nt"><parent></span>
<span class="nt"><groupId></span>org.springframework.boot<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>spring-boot-starter-parent<span class="nt"></artifactId></span>
<span class="nt"><version></span>2.0.5.RELEASE<span class="nt"></version></span>
<span class="nt"></parent></span>
<span class="nt"><dependencies></span>
<span class="c"><!-- Compile --></span>
<span class="nt"><dependency></span>
<span class="nt"><groupId></span>org.springframework.boot<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>spring-boot-starter-web<span class="nt"></artifactId></span>
<span class="nt"></dependency></span>
<span class="nt"><dependency></span>
<span class="nt"><groupId></span>org.springframework.boot<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>spring-boot-starter-thymeleaf<span class="nt"></artifactId></span>
<span class="nt"></dependency></span>
<span class="c"><!-- Test --></span>
<span class="nt"><dependency></span>
<span class="nt"><groupId></span>org.springframework.boot<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>spring-boot-starter-test<span class="nt"></artifactId></span>
<span class="nt"><scope></span>test<span class="nt"></scope></span>
<span class="nt"></dependency></span>
<span class="nt"></dependencies></span>
<span class="nt"><build></span>
<span class="nt"><plugins></span>
<span class="nt"><plugin></span>
<span class="nt"><groupId></span>org.springframework.boot<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>spring-boot-maven-plugin<span class="nt"></artifactId></span>
<span class="nt"></plugin></span>
<span class="nt"></plugins></span>
<span class="nt"></build></span>
<span class="nt"></project></span></code></pre></figure> <p>Those are the libraries you need to get started right. It is a web application, and you could use JSP here, but Spring docs kindly asks to avoid it due to some JSP limitations, then I decided to go with thymeleaf instead, another great template engine.</p> <p>Let’s start adding our test class. It will tell us what we expect to achieve in our implementantion, following a TDD approach.</p> <h4 id="class-hellotestjava">Class HelloTest.java</h4> <figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="n">springbootweb</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">static</span> <span class="n">org</span><span class="o">.</span><span class="na">junit</span><span class="o">.</span><span class="na">Assert</span><span class="o">.</span><span class="na">assertEquals</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">static</span> <span class="n">org</span><span class="o">.</span><span class="na">junit</span><span class="o">.</span><span class="na">Assert</span><span class="o">.</span><span class="na">assertTrue</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.junit.Test</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.junit.runner.RunWith</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.beans.factory.annotation.Autowired</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.boot.test.context.SpringBootTest</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.boot.test.context.SpringBootTest.WebEnvironment</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.boot.test.web.client.TestRestTemplate</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.http.HttpStatus</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.http.ResponseEntity</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.test.context.junit4.SpringRunner</span><span class="o">;</span>
<span class="nd">@RunWith</span><span class="o">(</span><span class="n">SpringRunner</span><span class="o">.</span><span class="na">class</span><span class="o">)</span>
<span class="nd">@SpringBootTest</span><span class="o">(</span><span class="n">webEnvironment</span> <span class="o">=</span> <span class="n">WebEnvironment</span><span class="o">.</span><span class="na">RANDOM_PORT</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">HelloTest</span> <span class="o">{</span>
<span class="nd">@Autowired</span>
<span class="kd">private</span> <span class="n">TestRestTemplate</span> <span class="n">restTemplate</span><span class="o">;</span>
<span class="nd">@Test</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testHelloController</span><span class="o">()</span> <span class="o">{</span>
<span class="n">ResponseEntity</span><span class="o"><</span><span class="n">String</span><span class="o">></span> <span class="n">entity</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">restTemplate</span><span class="o">.</span><span class="na">getForEntity</span><span class="o">(</span><span class="s">"/"</span><span class="o">,</span> <span class="n">String</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
<span class="n">assertEquals</span><span class="o">(</span><span class="n">HttpStatus</span><span class="o">.</span><span class="na">OK</span><span class="o">,</span> <span class="n">entity</span><span class="o">.</span><span class="na">getStatusCode</span><span class="o">());</span>
<span class="n">assertTrue</span><span class="o">(</span><span class="n">entity</span><span class="o">.</span><span class="na">getBody</span><span class="o">().</span><span class="na">contains</span><span class="o">(</span><span class="s">"Hi there!"</span><span class="o">));</span>
<span class="o">}</span>
<span class="o">}</span></code></pre></figure> <p>Here, we are using the <code class="highlighter-rouge">SpringRunner</code> that will provide our Spring application context, <code class="highlighter-rouge">SpringBootTest</code> to add additional infrastructure. In this case, it will provide the Web Application Context. To run the tests, there is a rest client inside <code class="highlighter-rouge">TestRestTemplate</code> class, and finally the code we need. It tests if we can successfully connect to “/” and if the page contains a “Hi there!” text.</p> <p>You can run the test by issuing:</p> <figure class="highlight"><pre><code class="language-sh" data-lang="sh"><span class="nv">$ </span>mvn clean
<span class="nv">$ </span>mvn <span class="nb">test</span></code></pre></figure> <p>It must result in failed test. And we fix it with next codes.</p> <blockquote> <blockquote> <p><img src="/images/20181229-error.png" alt="Noooo" class="img-responsive center-image" /></p> </blockquote> </blockquote> <p>Now, let’s add two simple classes here, one to make it a runnable Spring Boot application, and another for our controller:</p> <h4 id="class-applicationjava">Class Application.java</h4> <figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="n">springbootweb</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.boot.SpringApplication</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.boot.autoconfigure.SpringBootApplication</span><span class="o">;</span>
<span class="nd">@SpringBootApplication</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Application</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
<span class="n">SpringApplication</span><span class="o">.</span><span class="na">run</span><span class="o">(</span><span class="n">Application</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">args</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span></code></pre></figure> <p>Important things to note here, <code class="highlighter-rouge">@SpringBootApplication</code> annotation tells Spring that it is a starter class. <code class="highlighter-rouge">SpringApplication</code> class will effectivelly run our web application.’</p> <h4 id="class-mycontrollerjava">Class MyController.java</h4> <figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="n">springbootweb</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.Map</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.stereotype.Controller</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.web.bind.annotation.GetMapping</span><span class="o">;</span>
<span class="nd">@Controller</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyController</span> <span class="o">{</span>
<span class="nd">@GetMapping</span><span class="o">(</span><span class="s">"/"</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">welcome</span><span class="o">(</span><span class="n">Map</span><span class="o"><</span><span class="n">String</span><span class="o">,</span> <span class="n">Object</span><span class="o">></span> <span class="n">model</span><span class="o">)</span> <span class="o">{</span>
<span class="n">model</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">"message"</span><span class="o">,</span> <span class="s">"Hi there!"</span><span class="o">);</span>
<span class="k">return</span> <span class="s">"hello"</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span></code></pre></figure> <p>This small piece of code is enough to provide a call to <code class="highlighter-rouge">http://mydomain.com/</code>. It adds a message element to model <code class="highlighter-rouge">Map</code> and finally return the name of the view to render back to the user. Note that the view name is “<code class="highlighter-rouge">hello</code>”. Another important thing here is that we are using <code class="highlighter-rouge">@GetMapping</code> instead of <code class="highlighter-rouge">@RequestMapping</code> that you will normally find. This is a good practice, telling exactaly which HTTP methods are allowed there. It can also be done in <code class="highlighter-rouge">@RequestMapping</code> with <code class="highlighter-rouge">method</code> param.</p> <p>The last thing to add here is the view. In our case, we are using Thymeleaf, so here is how our page is coded:</p> <h4 id="view-templateshellohtml">View templates/hello.html</h4> <figure class="highlight"><pre><code class="language-html" data-lang="html"><span class="cp"><!DOCTYPE html></span>
<span class="nt"><html</span> <span class="na">xmlns=</span><span class="s">"http://www.w3.org/1999/xhtml"</span>
<span class="na">xmlns:th=</span><span class="s">"http://www.thymeleaf.org"</span><span class="nt">></span>
<span class="nt"><head></span>
<span class="nt"><meta</span> <span class="na">charset=</span><span class="s">"UTF-8"</span><span class="nt">></span>
<span class="nt"><title></span>Hello<span class="nt"></title></span>
<span class="nt"></head></span>
<span class="nt"><body></span>
<span class="nt"><span</span> <span class="na">th:text=</span><span class="s">"${message}"</span><span class="nt">></span>Say Hi!<span class="nt"></span></span>
<span class="nt"></body></span>
<span class="nt"></html></span></code></pre></figure> <p>If you are working with maven, and I hope you are and I can think about writing some cool post for that if are not, then you must place this <code class="highlighter-rouge">hello.html</code> file under src/main/resources/templates folder, that is the default location. Note that the name is the same as we are returning in controller. Otherwise, it will be under <code class="highlighter-rouge">WEB-INF</code> folder. The “Say Hi!” text is there as a fallback if thymeleaf can’t read message. We don’t want that message.</p> <h2 id="thats-all">That’s all</h2> <p>Ok, that is all the code you need. You can now run with</p> <figure class="highlight"><pre><code class="language-sh" data-lang="sh"><span class="nv">$ </span>mvn clean
<span class="nv">$ </span>mvn spring-boot:run</code></pre></figure> <p>It will start an embbeded tomcat container at 8080 port, and you can open your browser at <a href="http://localhost:8080"><code class="highlighter-rouge">http://localhost:8080</code></a>, and see a “Hey There!” beautiful page.</p> <blockquote> <blockquote> <p><img src="/images/20181229-hithere.png" alt="Hi There" class="img-responsive" /></p> </blockquote> </blockquote> <h2 id="conclusion">Conclusion</h2> <p>Ok, this is just a start. And a fast one, to build your spring boot app, and maybe your first Spring app. Don’t stop here. Get new materials, and there is a lot to come. In the next post, let’s add some more pages and something closer to a real app. You can get the code by forking this project at github. Don’t worry, each post will be a new repository.</p> <p><a href="https://github.com/edinhojorge/springbootweb-starter">Github repository</a></p> </div> </article> <footer class="site-footer"> <div class="container"> <ul class="social"> <li><a href="http://github.com/edinhojorge" target="_blank"><i class="icon icon-github"></i></a></li> <li><a href="http://linkedin.com/in/edinhojorge" target="_blank"><i class="icon icon-linkedin"></i></a></li> <li><a href="http://twitter.com/edinhojorge" target="_blank"><i class="icon icon-twitter"></i></a></li> </ul> <p> <small>©2018 All rights reserved. Made with <a href="http://jekyllrb.com/" target="_blank">Jekyll</a> and ♥</small> </p> </div> </footer> </main> <script src="/assets/js/jquery-2.1.1.js"></script> <script src="/assets/js/main.js"></script> </body> </html><!-- by nandomoreira.me -->