What is SpringBooot

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Requierements in this example
1.  Java 1.8
2. Eclipse JEE Luna

Started
Open Your Eclipse and Create New Maven Project

putracode-springboot-helloworld_1
putracode-springboot-helloworld_1

Fill the GroupId and ArtifactId

putracode-springboot-helloword_2
putracode-springboot-helloword_2

Adding Spring Boot Dependencies

Open up your project’s pom.xml and add Spring Boot as the parent of your project. Add Dependency for spring-boot-starter-web in order to the minimum required dependencies for writing a web application with Spring Boot.

<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.putracode</groupId>
<artifactId>SpringBoot_Part_1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.2.1.RELEASE</version>
</parent>

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
</dependencies>

</project>

The spring-boot-starter-parent is a special starter that provides useful Maven defaults. It also provides a dependency-management section so that you can omit version tags for “blessed” dependencies.Other “Starter POMs” simply provide dependencies that you are likely to need when developing aspecific type of application. Since we are developing a web application, we will add a spring-boot-starter-web dependency.

Add an Application class

package com.putracode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class MyBootSpring {

	public static void main(String[] args) {
		SpringApplication.run(MyBootSpring.class, args);
	}

}

MyBootSpring.java

The @EnableAutoConfiguration annotation
This annotation tells Spring Boot to “guess” how you will want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration will assume that you are developing a web application and setup Spring accordingly.

The main method has a single call to SpringApplication.run(MyBootSpring.class, args) which will start your application using Spring Boot defaults where it can.

Create a REST Controller
The next thing you may want to do at this point is add a controller to your application to handle servlet requests.

package com.putracode.rest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/putracode/**")
@RestController
public class HeloWorldController {
	@RequestMapping(method=RequestMethod.GET,produces={MediaType.APPLICATION_JSON_VALUE},value="/{_param}")
	public String getProcess(@PathVariable String _param)
	{
		return "Hello : "+_param;
	}
}

HeloWorldController.java

The first annotation on this controller is @RequestMapping(“/putracode/**”) and it tells us that this controller will handle all requests beginning with “/putracode” in our application.  The second annotation @RestController tells Spring two things.  First this class is a controller bean that should be managed by Spring and will be picked up by the @ComponentScan on our Application class.  Secondly, all methods inherently use @ResponseBody semantics which means the return value will become the body of our servlet response.

I’ve only defined a single method in this controller responding to the GET HTTP method,RequestMap, and it produces JSON output.  My method merely returns String of the request.  Your controller will return something else that’s related to your business case.

@RequestMapping(method=RequestMethod.GET,produces={MediaType.APPLICATION_JSON_VALUE},value="/{_param}")
public String getProcess(@PathVariable String _param){
return "Hello : "+_param;
}

In This Method can access by http by RequestMethod.GET and will return JSON value in your browser, With parameter /putracode/{_param}, {_param} this is parameter will be returned.

Running your Application MyBootSpring

At this point, you can run your application simply by right clicking inside your MyBootSpring class and selecting Run As Java Application .  Then browse to http://localhost:8080/putracode/_param and you’ll see the response from the controller and request mapping we just created.

If you’d like to view the source code of this application, it’s available on Github .

Happy Coding.. ^_^

Tagged with: