Searching...
Tuesday, 12 November 2013

Spring Boot Hello world Example

10:30

Spring  Boot API provides easy way of creating stand alone applications with minimum effort and very less configurations. It contains embedded tomcat or jetty to run web applications. Using this API no need to worry about creating war and other configuration. See more about Spring boot

Here is one simple web application which is using Spring boot to print hello world to the user.

1. Create a Maven Project and add below dependencies

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>0.5.0.BUILD-SNAPSHOT</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
	<repositories>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>http://repo.springsource.org/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>http://repo.springsource.org/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

2. Create a Controller class (HelloWorldController.java)


This is a controller class which is handling requests, here we are adding @EnableAutoConfiguration to take up auto configuration and in the main method using SpringApplication.run to create instance of the controller class.

package com.pretech;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class HelloWorldController {
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
      String welcome(String name) {
        return "Hello World Mr "+name;
    }
    public static void main(String[] args) throws Exception {
        SpringApplication.run(HelloWorldController.class, args);
    }
}

3. Run it


Run the above main program, your web application started via spring boot. Let us test this with below url

http://localhost:8080/?name=Pretech


image

0 comments: