Tuesday, 25 October 2016

How to create and execute a Spring boot project ?


  • Install eclipse Kepler
  • Create new Maven project
    • Check Create a simple project
    • Provide options : 
      •   Group Id = com.company
      •   Artifact Id = example1
      •   Packaging = war
    • Click create
    • It will create structure like :
      • # src/main/java 
      • # src/main/resources 
      • # src/test/java 
      • # src/test/resources 
      • > src/main/webapp 
      • > JRE system lib (Default JRE set in eclipse) 
      • > pom.xml

  • Provide Spring boot dependencies :
    <project ...>
       ...
       <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>1.4.1.RELEASE</version>
       </parent>

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

       <dependencies>
  </project>
    •  It will add all the Spring boot dependencies under Maven dependencies 
 
  • Create master application
    @SpringBootApplication
    public class Application {
       public static void main(String[] args) {
          SpringApplication.run(
Application.class, args);
       }
    }
  


  • Create Web service (REST) controller
    @RestController
    @RequestMapping ("/example1")

    public class WSController {
        @RequestMapping (path="/name", method=RequestMethod.GET)
        public String getName() {
            return "Hello World";
        }
    }

  • Run the application by right click on Application.java and choose "Run as Java application" as a Java application
    After embedded tomcat start, you can access resources using request path and controller :
    http://localhost:8080/example1/name

No comments:

Post a Comment

Note: only a member of this blog may post a comment.