Spring Boot with REST API implementations !!

Problem statement: Implementation of Spring boot rest api web application

How to create Spring Boot Project for Rest API web service own implementation
Method-I: 
step-1: go to eclipse / sts ide menu bar
step-2: select File --> New --> Spring Starter Project
step-3: by default pom.xml will add required spring boot dependency
step-4: create your own controller class and enjoy.

Method-II:
step-1: go to eclipse / sts ide menu bar
step-2: select File --> New --> Maven Project
step-3: add the <spring-boot-starter-parent >
step-4: add <spring-boot-starter-web> dependency and enjoy

  • Required code implementations:

[1]- pom.xml:
<project xmlns="http...>

        <groupId>com.ishaan</groupId>
<artifactId>maven_projects</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
       
        <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

        <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>

<dependencies>
<!-- This is a web application -->
<!-- Adds Tomcat and Spring MVC, along others, jackson-databind included
transitively -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Tomcat embedded container -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

[2] - SpringBootWebApplication .java:

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

//Create an Application class
@SpringBootApplication(scanBasePackages={"com.ishaan.springboot"})
// same as @Configuration @EnableAutoConfiguration @ComponentScan
public class SpringBootWebApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
System.out.println("Hello Spring Boot !!");
}
}

// @SpringBootApplication  add
// @Configuration -tags the class as a source of bean definitions for the application context.

// @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, //other beans, and various property settings.

[3] - WebServiceRestController .java:

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//Create a simple web application
@RestController
@RequestMapping("/api")
public class WebServiceRestController {

@RequestMapping(value = "/ping/", method = RequestMethod.GET)
public String ping() {
return "Ping success !!";
}
}

[4] - application.properties : (under src/main/resources)
# port number used for spring boot
server.port = 8090

[5] - application.yml: (under src/main/resources)                                        
        server:
            port: 8090
Note: Point [4] and point [5] are same thing, with different way of representation.

URL request to access resources: http://localhost:8090/api/ping/
Response from the TOMCAT server USING POSTMAN: Ping success !!


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation