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