This tutorial is designed for those who have not worked with the Spring Framework or rest web services before. I am keeping it simple and less verbose so that readers can easily follow the steps and start developing the restful web services using Spring Data Rest.
Spring Framework
is the trending and widely used J2EE frameworks. It has various modules for web development. In this tutorial, we will use the Spring Data Rest.
RESTful Web services
Representational State Transfer (REST) is based on server-client architecture and uses the HTTP protocol(stateless protocol). CRUD operations can be easily mapped on HTTP methods
Create
| POST |
Read | GET |
Update | PUT |
Delete | Delete |
Prerequisites for this tutorial
- Java 8 or later version
- Eclipse
- Maven
Let's start developing the RESTful web services
STEP 1
Open a new Maven Project in the eclipse, let's call it MyFirstRestApp. We will use the Spring Boot for this tutorial. Open the maven.xml file and add the following dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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>com.techieshah</groupId> | |
<artifactId>MyFirstRestApp</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.0.5.RELEASE</version> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-rest</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
</project> |
STEP 2
Create the MyRestApplication.java file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.techieshah; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
@SpringBootApplication | |
public class MyRestApplication { | |
public static void main( String[] args ) { | |
SpringApplication.run( MyRestApplication.class, args ); | |
} | |
} |
- @EnableAutoConfiguration
- @ComponentScan
- @Configuration
STEP 3
Now we will move to the Rest web services implementation. For simplicity, I am taking an example of Blog's posts. Create a BlogPostController.java file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.techieshah; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.springframework.web.bind.annotation.DeleteMapping; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import org.springframework.web.bind.annotation.PutMapping; | |
import org.springframework.web.bind.annotation.RequestBody; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import com.techieshah.domainmodel.BlogPost; | |
@RestController | |
@RequestMapping("/blogPosts") | |
public class BlogPostController { | |
private static List<BlogPost> blogPosts; | |
static { | |
blogPosts = new ArrayList<BlogPost>(); | |
blogPosts.add( new BlogPost( 1, "spring framework", "ws", "first example of rest web services" ) ); | |
blogPosts.add( new BlogPost( 2, "spring framework", "ws", "second example of rest web services" ) ); | |
} | |
@GetMapping("") | |
public List<BlogPost> all(){ | |
return blogPosts; | |
} | |
@GetMapping("/{id}") | |
public BlogPost get(@PathVariable Integer id){ | |
for(BlogPost blogPost : blogPosts) { | |
if(blogPost.id == id) { | |
return blogPost; | |
} | |
} | |
return null; | |
} | |
@PostMapping("") | |
public String post(@RequestBody BlogPost blogPost){ | |
blogPosts.add(blogPost); | |
return "Blog Post saved successfully"; | |
} | |
@PutMapping("") | |
public String put(@RequestBody BlogPost updatedBlogPost) { | |
for(BlogPost blogPost : blogPosts) { | |
if(blogPost.id == updatedBlogPost.id) { | |
blogPost.name = updatedBlogPost.name; | |
blogPost.tag = updatedBlogPost.tag; | |
blogPost.content = updatedBlogPost.content; | |
} | |
} | |
return "Blog Post updated successfully"; | |
} | |
@DeleteMapping("{id}") | |
public String delete(@PathVariable Integer id){ | |
for(BlogPost blogPost : blogPosts) { | |
if(blogPost.id == id) { | |
blogPosts.remove( blogPost ); | |
break; | |
} | |
} | |
return "Blog Post deleted successfully"; | |
} | |
} |
- @RestController tells the Spring framework that it is a RESTful web services resource.
- @RequestMapping("/blogPosts") routes every request to /blogPosts to this class
- HTTP methods are routed to the respective operations using the @GetMapping,@PostMapping,@PutMapping and @DeleteMapping annotations.
STEP 4
Run the application as Java application from eclipse
STEP 5
Now, let's test the application. We will use Postman ( a rest client) for this example.
The full source code for this tutorial can be found at MyFirstRestApp .
You will find the following tutorial useful
I hope that you found this tutorial useful. Please leave your feedback in the comments box below.
This comment has been removed by a blog administrator.
ReplyDelete