Spring Boot Hello World Application
Spring Boot is new project which gives solution to create convention over configuration stand alone application. Using this you could create production grade application deploy and just run. To take flavor of this please see how we create Hello World Application of Spring Boot:
- Create maven project name: SpringBootTest
- Final project structure:
- pom.xml:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.barcap.boot</groupId> <artifactId>SpringBootTest</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringBootTest Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.8.RELEASE</version> </parent> <dependencies> <!-- tag::jetty[] --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- end::jetty[] --> <!-- tag::actuator[] --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- end::actuator[] --> </dependencies> <properties> <start-class>hello.Application</start-class> </properties> <build> <finalName>SpringBootTest</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> <version>3.1</version> </plugin> </plugins> </build> </project>
- dispatcher-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.javahonk" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <!-- bind messages.properties --> <bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource"> <property name="basename" value="messages" /> </bean> </beans>
- index.jsp:
<!DOCTYPE html> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <html> <head lang="en"> <meta charset="utf-8"> <title>Spring Boot Hello World!</title> </head> <body> <h3>${message}</h3> <b>Sample message from resource bundle: <spring:message code="label.name" /></b> </body> </html>
- SpringBootController.java:
package com.javahonk; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; @RestController public class SpringBootController { private static final Logger logger = Logger.getLogger(SpringBootController.class); @RequestMapping(value = "/firstPage") public ModelAndView firstPage(ModelMap model) { logger.info("Log4j info is working"); logger.warn("Log4j warn is working"); logger.debug("Log4j debug is working"); logger.error("Log4j error is working"); System.out.println("System out is working"); model.addAttribute("message", "Spring Boot Hello World!"); return new ModelAndView("index"); } @RequestMapping("/springboot") public List<String> index() { List<String> testlist = new ArrayList<String>(); testlist.add("Greetings from Spring Boot!"); testlist.add("Greetings from Spring Boot!"); testlist.add("Greetings from Spring Boot!"); return testlist; } }
- Application.java:
package com.javahonk; import java.util.Arrays; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args); System.out.println("Let's inspect the beans provided by Spring Boot:"); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } } }
- For this demo we are running in eclipse with tomcat sever and if you are not sure how to configure run tomcat in eclipse please follow this tutorial. To run: right click project –> Run As –> Run on server there you will see below output:
- Type below URL in browser you will see below output:
- Now send HTTP request to controller as RESTFul you will get below JSON response:
Now run stand alone application:
- Right click Application.java –> Run As –> Java Application you will below output:
- That’s it for more information about Spring Boot please read this tutorial
Download Project: SpringBootTest
Hi sir,
First I need to say big thanks to you,for sharing valuable information in the form of this site.Many more technologies information is available for beginners as well as for advanced learners. Related to spring boot example , i am downloaded your code .when it is running as web server it is working fine.After server stopped tried as standalone .But in case running as standalone, no errors is coming in console ,when i invoking url(http://localhost:8080/SpringBootTest/springboot) in the browser it not getting the desired output. Can you please suggest me what is the way to run the example without starting server using as Run as server.
My console Output:
. ____ _ __ _ _
/\\ / ___’_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
‘ |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.1.8.RELEASE)
2014-12-17 13:03:42.912 INFO 2764 — [ main] com.javahonk.Application : Starting Application on SON16024 with PID 2764 (C:\Users\ramanujam.as\Downloads\SpringBootTest\SpringBootTest\target\classes started by ramanujam.as in C:\Users\ramanujam.as\Downloads\SpringBootTest\SpringBootTest)
2014-12-17 13:03:42.953 INFO 2764 — [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@73fa5e49: startup date [Wed Dec 17 13:03:42 IST 2014]; root of context hierarchy
2014-12-17 13:03:44.520 INFO 2764 — [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2014-12-17 13:03:44.548 INFO 2764 — [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Registering beans for JMX exposure on startup
2014-12-17 13:03:44.825 INFO 2764 — [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2014-12-17 13:03:44.827 INFO 2764 — [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean ‘requestMappingEndpoint’: registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=requestMappingEndpoint]
2014-12-17 13:03:44.849 INFO 2764 — [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean ‘environmentEndpoint’: registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=environmentEndpoint]
2014-12-17 13:03:44.857 INFO 2764 — [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean ‘healthEndpoint’: registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=healthEndpoint]
2014-12-17 13:03:44.863 INFO 2764 — [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean ‘beansEndpoint’: registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=beansEndpoint]
2014-12-17 13:03:44.868 INFO 2764 — [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean ‘infoEndpoint’: registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=infoEndpoint]
2014-12-17 13:03:44.874 INFO 2764 — [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean ‘metricsEndpoint’: registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=metricsEndpoint]
2014-12-17 13:03:44.880 INFO 2764 — [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean ‘traceEndpoint’: registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=traceEndpoint]
2014-12-17 13:03:44.885 INFO 2764 — [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean ‘dumpEndpoint’: registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=dumpEndpoint]
2014-12-17 13:03:44.894 INFO 2764 — [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean ‘autoConfigurationAuditEndpoint’: registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=autoConfigurationAuditEndpoint]
2014-12-17 13:03:44.899 INFO 2764 — [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean ‘shutdownEndpoint’: registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=shutdownEndpoint]
2014-12-17 13:03:44.907 INFO 2764 — [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean ‘configurationPropertiesReportEndpoint’: registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=configurationPropertiesReportEndpoint]
2014-12-17 13:03:44.913 INFO 2764 — [ main] com.javahonk.Application : Started Application in 2.39 seconds (JVM running for 2.671)
Let’s inspect the beans provided by Spring Boot:
application
auditEventRepository
auditListener
autoConfigurationAuditEndpoint
beansEndpoint
configurationPropertiesReportEndpoint
counterService
dumpEndpoint
endpointMBeanExporter
endpoints.jmx.CONFIGURATION_PROPERTIES
environmentEndpoint
gaugeService
healthAggregator
healthEndpoint
http.mappers.CONFIGURATION_PROPERTIES
infoEndpoint
jacksonObjectMapper
managementServerProperties
mappingJackson2HttpMessageConverter
mbeanExporter
mbeanServer
messageConverters
messageSource
metricRepository
metricsEndpoint
objectNamingStrategy
org.springframework.boot.actuate.autoconfigure.AuditAutoConfiguration
org.springframework.boot.actuate.autoconfigure.AuditAutoConfiguration$AuditEventRepositoryConfiguration
org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration
org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration$InfoPropertiesConfiguration
org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration$RequestMappingEndpointConfiguration
org.springframework.boot.actuate.autoconfigure.EndpointMBeanExportAutoConfiguration
org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration
org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration
org.springframework.boot.actuate.autoconfigure.MetricRepositoryAutoConfiguration
org.springframework.boot.actuate.autoconfigure.MetricRepositoryAutoConfiguration$MetricRepositoryConfiguration
org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration
org.springframework.boot.autoconfigure.AutoConfigurationPackages
org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperAutoConfiguration
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration$Empty
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration$ObjectMappers
org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor
org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.store
org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
org.springframework.context.annotation.MBeanExportConfiguration
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
propertySourcesPlaceholderConfigurer
requestMappingEndpoint
shutdownEndpoint
springBootController
statusHealthIndicator
traceEndpoint
traceRepository
2014-12-17 13:03:44.915 INFO 2764 — [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@73fa5e49: startup date [Wed Dec 17 13:03:42 IST 2014]; root of context hierarchy
2014-12-17 13:03:44.916 INFO 2764 — [ Thread-1] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 0
2014-12-17 13:03:44.918 INFO 2764 — [ Thread-1] o.s.b.a.e.jmx.EndpointMBeanExporter : Unregistering JMX-exposed beans on shutdown
2014-12-17 13:03:44.919 INFO 2764 — [ Thread-1] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
Thanks in Advance
Ramanujam