Spring restful web services example step by step
In this tutorial, we are going to create Spring restful web services step by step.
We will create a simple Spring restful web services which will return JSON as a response.
Here is project structure for the application.
Step 1: Create a dynamic web project using maven in eclipse.
Maven configuration
Step 2: Add require dependencies to pom.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.dev.code2master</groupId> <artifactId>SpringRestfulWebServicesExample</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringRestfulWebServicesExample Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>5.0.1.RELEASE</spring.version> <jdk.version>1.8</jdk.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.6</version> </dependency> </dependencies> <build> <finalName>SpringRestfulWebServicesExample</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> </plugins> </build> </project> |
Please note that we have put “jackson-databind” as dependency here, so spring will load Jackson2JsonMessageConverter into the classpath,so whenever we have any requst with “application/json”,Jackson2JsonMessageConverter will take care of required conversions.
Web configuration
Step 3: Change web.xml as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>springrestweb</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springrestweb</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> |
Step 4: create a xml file called springrestweb-servlet.xml in /WEB-INF/ folder.Please note that file name should be “$servlent-name” with “-servlet” as prefix.
Please change context:component-scan if you want to use different package for spring to search for controller.
1 2 3 4 5 6 7 8 9 10 11 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="org.dev.code2master.controller" /> </beans> |
Create model
Step 5: Create model class Customer.java as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package org.dev.code2master.model; public class Customer { int id; String name; int age; public Customer(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
Create controller
Step 6: Create controller class named CustomerController.java as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package org.dev.code2master.controller; import java.util.ArrayList; import java.util.List; import org.dev.code2master.model.Customer; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class CustomerController { @RequestMapping(value = "/customers", method = RequestMethod.GET,headers="Accept=application/json") public List<Customer> getCustomers() { List<Customer> customerList=createCustomerList(); return customerList; } @RequestMapping(value = "/customer/{id}", method = RequestMethod.GET,headers="Accept=application/json") public Customer getCustomerById(@PathVariable int id) { List<Customer> customerList=createCustomerList(); for (Customer customer: customerList) { if(customer.getId()==id) return customer; } return null; } /// Utiliy method to create customer list. public List<Customer> createCustomerList() { Customer customer1=new Customer(1, "Mohan",20); Customer customer2=new Customer(4, "Ram",43); Customer customer3=new Customer(3, "John",48); Customer customer4=new Customer(2, "Martin",29); List<Customer> listOfCustomers = new ArrayList<>(); listOfCustomers.add(customer1); listOfCustomers.add(customer2); listOfCustomers.add(customer3); listOfCustomers.add(customer4); return listOfCustomers; } } |
@RequestMapping annotation is used to map web requests to controller method.
For example:
If we have request as “http://localhost:8080/SpringRestfulWebServicesExample/customers” then getCustomers will be called from above controller.
Please note that we have used @RestConstroller to get resource as response.We don’t need to specify @ResponseBody any more.
In spring 4.0, we can use @RestController which is combination of @Controller + @ResponseBody.
Build maven project
Step 7: Build your maven project.
Right click on project -> Run as -> Maven build
Step 8: Put “clean install” as maven goals.
click on run.
Run the application
Step 9:Run the application.
Right click on project -> run as -> run on server
Select apache tomcat and click on finish
Browser REST URLs
Step 10: Browse below URL
URL: “http://localhost:8080/SpringRestfulWebServicesExample/customers”
Now let’s get customer by id.
URL: “http://localhost:8080/SpringRestfulWebServicesExample/customer/3”
Source code
Source code url for spring restful web services example step by step
That’s all about spring restful web services example step by step.