Simple SpringBoot RestAPI Example:
Spring Boot is a way to create stand-alone applications with less or no configurations and has defaults for annotation configuration which we will use in our application.
In this example, Let us a see a very simple SpringBoot with one RESTAPI url. This example does not have any database connection or pojo classes.
POM.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring.first</groupId> <artifactId>mavenproject1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> </parent> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <compilerVersion>${javac.target}</compilerVersion> <source>${javac.target}</source> <target>${javac.target}</target> <compilerArgument>-Xlint</compilerArgument> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.5</version> <configuration> <filesets> <fileset> <directory>.</directory> <includes> <include>**/*~</include> </includes> </fileset> </filesets> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.10.RELEASE</version> <type>jar</type> </dependency> </dependencies> <name>SpringBootRESTApi</name> </project>
SpringController.java
package SpringBootRestAPI; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * * @author vikram */ @RestController public class SpringController { @RequestMapping(value = "/SampleData",method = RequestMethod.GET) public String sampleData(@RequestParam("name") String name){ String Hello = "Hello Spring Boot Rest API "+name; return Hello; } }
SpringRest.java
package SpringBootRestAPI; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * * @author vikram */ @SpringBootApplication public class SpringRest { public static void main(String args[]){ SpringApplication.run(SpringRest.class, args); } }
Let us start the spring boot application,
Hitting the URL with PostMan,