-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeed.xml
More file actions
215 lines (159 loc) · 28 KB
/
feed.xml
File metadata and controls
215 lines (159 loc) · 28 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Eder Luis Jorge</title>
<description>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. </description>
<link>http://localhost:4000/</link>
<atom:link href="http://localhost:4000/feed.xml" rel="self" type="application/rss+xml"/>
<pubDate>Mon, 31 Dec 2018 17:37:59 -0200</pubDate>
<lastBuildDate>Mon, 31 Dec 2018 17:37:59 -0200</lastBuildDate>
<generator>Jekyll v3.8.5</generator>
<item>
<title>Spring time, Spring boot, Spring Framework</title>
<description><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">&lt;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">&gt;</span>
<span class="nt">&lt;modelVersion&gt;</span>4.0.0<span class="nt">&lt;/modelVersion&gt;</span>
<span class="nt">&lt;groupId&gt;</span>com.github.edinhojorge.spring<span class="nt">&lt;/groupId&gt;</span>
<span class="nt">&lt;artifactId&gt;</span>springbootweb-starter<span class="nt">&lt;/artifactId&gt;</span>
<span class="nt">&lt;version&gt;</span>0.0.1-SNAPSHOT<span class="nt">&lt;/version&gt;</span>
<span class="nt">&lt;packaging&gt;</span>war<span class="nt">&lt;/packaging&gt;</span>
<span class="nt">&lt;name&gt;</span>Spring boot web<span class="nt">&lt;/name&gt;</span>
<span class="nt">&lt;description&gt;</span>A small spring boot app, showing the first steps for this framework<span class="nt">&lt;/description&gt;</span>
<span class="nt">&lt;url&gt;</span>https://edinhojorge.github.com<span class="nt">&lt;/url&gt;</span>
<span class="nt">&lt;parent&gt;</span>
<span class="nt">&lt;groupId&gt;</span>org.springframework.boot<span class="nt">&lt;/groupId&gt;</span>
<span class="nt">&lt;artifactId&gt;</span>spring-boot-starter-parent<span class="nt">&lt;/artifactId&gt;</span>
<span class="nt">&lt;version&gt;</span>2.0.5.RELEASE<span class="nt">&lt;/version&gt;</span>
<span class="nt">&lt;/parent&gt;</span>
<span class="nt">&lt;dependencies&gt;</span>
<span class="c">&lt;!-- Compile --&gt;</span>
<span class="nt">&lt;dependency&gt;</span>
<span class="nt">&lt;groupId&gt;</span>org.springframework.boot<span class="nt">&lt;/groupId&gt;</span>
<span class="nt">&lt;artifactId&gt;</span>spring-boot-starter-web<span class="nt">&lt;/artifactId&gt;</span>
<span class="nt">&lt;/dependency&gt;</span>
<span class="nt">&lt;dependency&gt;</span>
<span class="nt">&lt;groupId&gt;</span>org.springframework.boot<span class="nt">&lt;/groupId&gt;</span>
<span class="nt">&lt;artifactId&gt;</span>spring-boot-starter-thymeleaf<span class="nt">&lt;/artifactId&gt;</span>
<span class="nt">&lt;/dependency&gt;</span>
<span class="c">&lt;!-- Test --&gt;</span>
<span class="nt">&lt;dependency&gt;</span>
<span class="nt">&lt;groupId&gt;</span>org.springframework.boot<span class="nt">&lt;/groupId&gt;</span>
<span class="nt">&lt;artifactId&gt;</span>spring-boot-starter-test<span class="nt">&lt;/artifactId&gt;</span>
<span class="nt">&lt;scope&gt;</span>test<span class="nt">&lt;/scope&gt;</span>
<span class="nt">&lt;/dependency&gt;</span>
<span class="nt">&lt;/dependencies&gt;</span>
<span class="nt">&lt;build&gt;</span>
<span class="nt">&lt;plugins&gt;</span>
<span class="nt">&lt;plugin&gt;</span>
<span class="nt">&lt;groupId&gt;</span>org.springframework.boot<span class="nt">&lt;/groupId&gt;</span>
<span class="nt">&lt;artifactId&gt;</span>spring-boot-maven-plugin<span class="nt">&lt;/artifactId&gt;</span>
<span class="nt">&lt;/plugin&gt;</span>
<span class="nt">&lt;/plugins&gt;</span>
<span class="nt">&lt;/build&gt;</span>
<span class="nt">&lt;/project&gt;</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">&lt;</span><span class="n">String</span><span class="o">&gt;</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">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">Object</span><span class="o">&gt;</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">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;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">&gt;</span>
<span class="nt">&lt;head&gt;</span>
<span class="nt">&lt;meta</span> <span class="na">charset=</span><span class="s">"UTF-8"</span><span class="nt">&gt;</span>
<span class="nt">&lt;title&gt;</span>Hello<span class="nt">&lt;/title&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
<span class="nt">&lt;span</span> <span class="na">th:text=</span><span class="s">"${message}"</span><span class="nt">&gt;</span>Say Hi!<span class="nt">&lt;/span&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</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>
</description>
<pubDate>Sat, 29 Dec 2018 00:00:00 -0200</pubDate>
<link>http://localhost:4000/2018/spring-boot-spring-time-spring-framework/</link>
<guid isPermaLink="true">http://localhost:4000/2018/spring-boot-spring-time-spring-framework/</guid>
</item>
</channel>
</rss>