Create a new Spring Boot Maven Application

Create a new Spring Boot Maven Application 

mvn archetype:generate -DgroupId=com.splitwise -DartifactId=SplitwiseAssignment -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
The above will create a sample application 

Open this in intellij 

Then add the following dependencies :


Parent add :
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.9.RELEASE</version>
</parent>

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

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.10</version>
</dependency>


<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

To run the application : 

mvn spring-boot:run

Simple controller class

@RestControllerpublic class HelloWorld {

    // Simple get api with out params    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }


    // Get api with params    @RequestMapping(path = "/greeting",method= RequestMethod.GET)
    public String greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return "Hello " + name;
    }

    // Get api object response    @RequestMapping(path = "/greeting",method= RequestMethod.GET)
    public Greeting greetingNew(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(name);
    }


}

Create a new package and ensure to add the following in the main method.
@ComponentScan("package name")@SpringBootApplicationpublic class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class, args);
    }
}

Comments