Steps to create Spring MVC project in STS with Maven

This page will help you to learn, what are the steps we need to follow to create Spring MVC project with the support of STS and Maven. There are couple of ways creating the WEB project. This is one of the way of creating as Maven Project.

File ==> New ==> Others

Search for Maven and choose Maven Project.

No need to do anything in below screen, click on Next

Search for webapp in Filter section and select the below show webapp

Enter the Group Id and Artifact as whatever you like,

Cloud Foundry – Create First Application

As of now, we have covered the concepts like download and install CF – CLI, Login into Space from CLI. Now, we look at how to create our first application to deploy into Cloud Foundry Space. Here I am explaining how to create a basic Spring Boot application from Spring Initilizr to deploy into Cloud Foundry. If you already have an application ready to deploy into Cloud Foundry, you can ignore this post. Otherwise please follow the below.
Create and download a project from Spring Initializr by choosing the modules like WEB, DEVTOOLS, REST REPOSITORIES. Then extract into specific location and import it into your IDE(Eclipse, Intellij, etc) to do small modifications.

Then create a Controller class which is annotated with @RestController and add a method which is also annotated with @GetMapping(path=”/greet/{name}”). It look like below,

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetService {

 @GetMapping(path = "/greet/{name}")
 public String greetWithName(@PathVariable String name) {

  return "Hello " + name + "... Welcome to Cloud Foundry :)";

 }

}

Add the @EnableAutoConfiguration along with existing @SpringBootApplication to the main class of the application. So, it will get identified all the components and controllers present in the application. My main class look like below,

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableAutoConfiguration
@SpringBootApplication
public class CfGreetApplication {

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

Now, Run your application and access the API present in Controller/Service class. As per the above code http://localhost:8080/greet/javaisocean
As on when you hit the above API in browser, you will get the response saying Hello javaisocean… Welcome to Cloud Foundry 🙂

In coming post we will look at how to deploy this application into Cloud Foundry. Prior to that, we need to create a Jar file of this application by using mvn clean install.

Previous: Cloud Foundry Commands

Next: Cloud Foundry – Push application into Space