{"id":41600,"date":"2016-10-14T16:09:15","date_gmt":"2016-10-14T10:39:15","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=41600"},"modified":"2016-12-19T15:04:03","modified_gmt":"2016-12-19T09:34:03","slug":"spring-boot-explained-2","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/spring-boot-explained-2\/","title":{"rendered":"An overview of Spring Boot"},"content":{"rendered":"<p>I have come up with this blog that outlines:<\/p>\n<ol>\n<li style=\"text-align: left;\">What is Spring Boot?<\/li>\n<li style=\"text-align: left;\">Why do we use it?<\/li>\n<li style=\"text-align: left;\">How to get started?<\/li>\n<\/ol>\n<p><strong>1. What is Spring Boot?<\/strong><\/p>\n<p>Spring boot from Spring is just another project enabling\u00a0developers to create stand-alone, production-grade Spring based applications.<\/p>\n<p><strong>2. Why do we use it?<\/strong><\/p>\n<ol>\n<li>Create stand-alone Spring applications<\/li>\n<li>Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files). You can create a war file and deploy if\u00a0need be.<\/li>\n<li>Provide opinionated &#8216;starter&#8217; POMs to simplify your Maven configuration<\/li>\n<li>Absolutely <strong>no code generation<\/strong> and <strong>no requirement for XML<\/strong> configuration (Automatically configure spring wherever possible)<\/li>\n<\/ol>\n<p><strong>3. How to get started?<\/strong><\/p>\n<p>There are multiple ways you can get started with spring boot application eg &#8211; Spring Boot CLI, Maven or gradle build tool.<\/p>\n<p>I am going to use Eclipse + Maven. However, if you prefer using\u00a0gradle or boot client there are many examples that you can find &#8211; <a title=\"Here is one for CLI\" href=\"http:\/\/www.tothenew.com\/blog\/quick-start-with-springboot-using-cli\/\">Here is one with CLI<\/a> .<\/p>\n<p>Let&#8217;s get started &#8211;<\/p>\n<ol>\n<li>Create a maven project with following archetype<\/li>\n<\/ol>\n<pre>&lt;groupId&gt;am.ik.archetype&lt;\/groupId&gt;\r\n&lt;artifactId&gt;spring-boot-blank-archetype&lt;\/artifactId&gt;\r\n&lt;version&gt;1.0.6&lt;\/version\r\n<\/pre>\n<p>2. Now you will get default pom.xml file with many dependencies ( these dependencies are not required as it&#8217;s just an example, so I am removing all dependencies except the one outlined below)\u00a0&#8211;<\/p>\n<pre>&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n&lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId<\/pre>\n<p>Here is how your maven may look like &#8211;<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\r\n&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n\r\n&lt;groupId&gt;com.ttn.aem&lt;\/groupId&gt;\r\n&lt;artifactId&gt;springboot&lt;\/artifactId&gt;\r\n&lt;version&gt;1.0.0&lt;\/version&gt;\r\n&lt;packaging&gt;jar&lt;\/packaging&gt;\r\n\r\n&lt;name&gt;Spring Boot Blank Project (from ;\r\n\r\n&lt;parent&gt;\r\n&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n&lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;\r\n&lt;version&gt;1.4.1.RELEASE&lt;\/version&gt;\r\n&lt;\/parent&gt;\r\n\r\n&lt;properties&gt;\r\n&lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;\r\n&lt;!-- &lt;start-class&gt;com.ttn.aem.springboot.main.App&lt;\/start-class&gt; --&gt;\r\n&lt;java.version&gt;1.8&lt;\/java.version&gt;\r\n&lt;lombok.version&gt;1.14.8&lt;\/lombok.version&gt;\r\n&lt;log4jdbc.log4j2.version&gt;1.16&lt;\/log4jdbc.log4j2.version&gt;\r\n&lt;rest.assured.version&gt;2.3.3&lt;\/rest.assured.version&gt;\r\n&lt;\/properties&gt;\r\n\r\n&lt;dependencies&gt;\r\n&lt;dependency&gt;\r\n&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n&lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\r\n&lt;\/dependency&gt;\r\n&lt;\/dependencies&gt;\r\n&lt;build&gt;\r\n&lt;finalName&gt;spring-boot-${project.version}&lt;\/finalName&gt;\r\n&lt;plugins&gt;\r\n&lt;plugin&gt;\r\n&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n&lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;\r\n&lt;dependencies&gt;\r\n&lt;dependency&gt;\r\n&lt;groupId&gt;org.springframework&lt;\/groupId&gt;\r\n&lt;artifactId&gt;springloaded&lt;\/artifactId&gt;\r\n&lt;version&gt;${spring-loaded.version}&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n&lt;\/dependencies&gt;\r\n&lt;\/plugin&gt;\r\n&lt;\/plugins&gt;\r\n&lt;\/build&gt;\r\n\r\n&lt;\/project&gt;\r\n<\/pre>\n<p>3. You will get 3 generated classes. Out of these, we need only App.java class which we will use as a starter class for our application. Here is how your App.java will look\u00a0\u00a0&#8211;<\/p>\n<p>package com.ttn.aem.springboot.main;<\/p>\n<p>import org.springframework.boot.SpringApplication;<br \/>\nimport org.springframework.boot.autoconfigure.SpringBootApplication;<\/p>\n<pre>@SpringBootApplication(scanBasePackages=\"com.ttn.aem.springboot.controllers\")\r\npublic class App {\r\n    public static void main(String[] args) {\r\n        SpringApplication.run(App.class, args);\r\n    }\r\n}\r\n<\/pre>\n<p>@SpringBootApplication works as entry point for the application and bootstrap spring. When applicaiton starts spring boot search for the configuration annotation.<\/p>\n<p>We can also use @EnableAutoConfiguration and @ComponentScan instead of @SpringBootApplication, depending on the preferences.<\/p>\n<p>We have also given scanBasePackages along with @SpringBootApplication so that it is easy to\u00a0look for the configuration or the classes for which beans needs to be created.<\/p>\n<p>4. Here is our controller code &#8211;<\/p>\n<pre>package com.ttn.aem.springboot.controllers;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.web.bind.annotation.RequestMapping;\r\nimport org.springframework.web.bind.annotation.RequestParam;\r\nimport org.springframework.web.bind.annotation.RestController;\r\n\r\n@RestController\r\npublic class HelloController {\r\n\r\n    @RequestMapping(\"\/\")\r\n    public String hello() {\r\n        return \"Hello World!\";\r\n    }\r\n\r\n    @RequestMapping(\"calc\")\r\n    public int calc(@RequestParam int left, @RequestParam int right) {\r\n        return left+right;\r\n    }\r\n}\r\n<\/pre>\n<p>RestController is pretty straight forward isn&#8217;t it?<\/p>\n<p>And we already have instructed spring to scan com.ttn.aem.springboot.controllers package<\/p>\n<p>What&#8217;s next?<\/p>\n<p>That&#8217;s all &#8211; Our application is ready to build and run.<\/p>\n<pre><span style=\"color: #ff0000;\">run mvn clean instal<\/span><\/pre>\n<p>Then<\/p>\n<pre><span style=\"color: #ff0000;\">java -jar target\/&lt;jarname&gt;.jar<\/span><\/pre>\n<p>Now go to\u00a0the below link:<\/p>\n<p>http:\/\/localhost:8000\/calc?left=10&amp;right=5 and http:localhost:8080<\/p>\n<p>You see it works. Was it ever so easy to create a spring rest application?<\/p>\n<ul>\n<li>No Web.xml<\/li>\n<li>No dispatcher-servlet.xml<\/li>\n<li>No applicationContext.xml<\/li>\n<li>No Annotation configuration classes<\/li>\n<li>and the list goes on.<\/li>\n<\/ul>\n<p>Please share your thoughts in the comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have come up with this blog that outlines: What is Spring Boot? Why do we use it? How to get started? 1. What is Spring Boot? Spring boot from Spring is just another project enabling\u00a0developers to create stand-alone, production-grade Spring based applications. 2. Why do we use it? Create stand-alone Spring applications Embed Tomcat, [&hellip;]<\/p>\n","protected":false},"author":992,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[446,1],"tags":[4844,4841,1202],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/41600"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/992"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=41600"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/41600\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=41600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=41600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=41600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}