create account

How to develop Rest Web Service using @RestController - Spring Framework by ravik2492

View this thread on: hive.blogpeakd.comecency.com
· @ravik2492 · (edited)
$0.04
How to develop Rest Web Service using @RestController - Spring Framework
![](https://www.javatpoint.com/images/spimages/spring1.png)

Source[Here](https://www.javatpoint.com/images/spimages/spring1.png)
#### What Will I Learn?
In this tutorial, we will learn about how we can develop Rest web API or Services using @RestController annotation in Spring framework and how  you can call the web services using ARC Client

- Rest Web services
- Get JSON data from Rest web Services using ARC or Web Browser

#### Requirements

- Eclipse IDE (Juno or above)
- JDK 1.7 or above
- Tomcat Server 7 or above
- Spring and jackson jar files
- Web Browser Chrome or Firefox

#### Difficulty

- Basic

#### Tutorial Contents
- Application Configuration wit Spring Framework

Now we configure the web application with sping framework to creating @restController

1. configuring web.xml file
First of all paste all jar files into lib folder
Here we configure web.xml file to run application application
```
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
  </servlet>  
  <servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
		<welcome-file></welcome-file>
  </welcome-file-list>  
</web-app>
```
![Screenshot (103).png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012856/sl4tmx5pf01oo2jkqofb.png)

1. configuring spring-servlet.xml
Basic configuration of databse, view resolver and sessionfactory
```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
```
```	
<context:property-placeholder location="classpath:resources/database.properties" />
	<context:component-scan base-package="com.blog" />
```
```
	<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean>
	```
```
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="annotatedClasses">
			<list>
				<value>com.blog.model.Users</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>				
			</props>
		</property>
	</bean>
```

</beans>
```
![Screenshot (104).png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012832/wq4boushyywgq6yoplrb.png)



- Creating RestController
Here we create RestControler java file and write code for populating list and converlist from ArrayList to json and this controler return json object string

```
package com.blog.restwebservice;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.blog.bean.QuestionsBean;
import beans.ConvertDateFormat;
import beans.ListToJsonObject;
import beans.Sudent;
import com.blog.model.Questions;
import com.blog.service.QuestionsService;

@RestController
@RequestMapping("/yayayaa")
public class RestWebServiceController {
	@Autowired(required=false)
	private QuestionsService questionsService;	
	ListToJsonObject listtojson = new ListToJsonObject();
	ConvertDateFormat fmt = new ConvertDateFormat();	
	@RequestMapping(method = RequestMethod.GET)
	public @ResponseBody String listQuestionsss() throws JsonGenerationException, JsonMappingException, IOException, ParseException {						
		List<Sudent> list = listofStudents();	
		String json = new ObjectMapper().writeValueAsString(list);
		return json;
	}		
	List<Sudent> list = new ArrayList<Sudent>();
	public List<Sudent> listofStudents()
	{
		list.add(new Sudent(1,20,"Rahul","Whatever","rahul@gmail.com"));
		list.add(new Sudent(2,21,"Ramesh","Whatever","ramesh@gmail.com"));
		list.add(new Sudent(3,22,"Shyam","Whatever","shyam@gmail.com"));
		list.add(new Sudent(4,23,"Ram","Whatever","ram@gmail.com"));
		list.add(new Sudent(5,24,"Krishna","Whatever","krishna@gmail.com"));
		list.add(new Sudent(6,25,"Jacop","Whatever","jacop@gmail.com"));		
		return list;
	}
}
```
![Screenshot (106).png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012892/bhzpvotg0rsp48hef9sv.png)


### Result
- Lets  run the project using tomcat server call the controller using ARC Client
![Screenshot (108).png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012493/gazows1dozstbkjuewtv.png)

- run on web browser
![Screenshot (107).png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012563/lfotzpxpatt59jyv3xqf.png)

- parse retuned JSON data with json parser
![Screenshot (109).png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012668/temdozmcxfwl8xaud9xv.png)


 It's Done !
Congratulations ! we successfully developed Rest Web Service.
#### Thank You !
Share from the core of my heart

    

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@ravik2492/how-to-develop-rest-web-service-using-restcontroller-spring-framework">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , ,
properties (23)
authorravik2492
permlinkhow-to-develop-rest-web-service-using-restcontroller-spring-framework
categoryutopian-io
json_metadata{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":115827867,"name":"utopian-io","full_name":"Ravik2492/utopian-io","html_url":"https://github.com/Ravik2492/utopian-io","fork":false,"owner":{"login":"Ravik2492"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","utopian-io","spring","restwebservices","java"],"users":["RestController","restController","RequestMapping","Autowired","ResponseBody","gmail.com"],"links":["https://www.javatpoint.com/images/spimages/spring1.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012856/sl4tmx5pf01oo2jkqofb.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012832/wq4boushyywgq6yoplrb.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012892/bhzpvotg0rsp48hef9sv.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012493/gazows1dozstbkjuewtv.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012563/lfotzpxpatt59jyv3xqf.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012668/temdozmcxfwl8xaud9xv.png"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012856/sl4tmx5pf01oo2jkqofb.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012832/wq4boushyywgq6yoplrb.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012892/bhzpvotg0rsp48hef9sv.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012493/gazows1dozstbkjuewtv.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012563/lfotzpxpatt59jyv3xqf.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012668/temdozmcxfwl8xaud9xv.png"],"moderator":{"account":"manishmike10","time":"2018-01-16T14:08:22.426Z","flagged":true,"reviewed":false,"pending":false}}
created2018-01-15 10:42:45
last_update2018-01-16 14:08:21
depth0
children3
last_payout2018-01-22 10:42:45
cashout_time1969-12-31 23:59:59
total_payout_value0.043 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,922
author_reputation1,501,149,936,516
root_title"How to develop Rest Web Service using @RestController - Spring Framework"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id29,672,914
net_rshares6,464,489,218
author_curate_reward""
vote details (10)
@manishmike10 ·
Your contribution cannot be approved because it does not follow the [Utopian Rules](https://utopian.io/rules).
* Wrong repository 
Rest controller is not open_ Source.
You can contact us on [Discord](https://discord.gg/uTyJkNm).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authormanishmike10
permlinkre-ravik2492-how-to-develop-rest-web-service-using-restcontroller-spring-framework-20180116t141233265z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-16 14:12:36
last_update2018-01-16 14:12:36
depth1
children0
last_payout2018-01-23 14:12:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length285
author_reputation20,399,732,899,016
root_title"How to develop Rest Web Service using @RestController - Spring Framework"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id29,949,148
net_rshares0
@neelamgahlyan ·
Great work. Make a complete tutorial
properties (22)
authorneelamgahlyan
permlinkre-ravik2492-how-to-develop-rest-web-service-using-restcontroller-spring-framework-20180115t104414760z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-15 10:44:15
last_update2018-01-15 10:44:15
depth1
children1
last_payout2018-01-22 10:44:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length36
author_reputation4,455,241,403,180
root_title"How to develop Rest Web Service using @RestController - Spring Framework"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id29,673,173
net_rshares0
@ravik2492 ·
Thank you ! 2 hours to complete the work and half hour to write tutorial
properties (22)
authorravik2492
permlinkre-neelamgahlyan-re-ravik2492-how-to-develop-rest-web-service-using-restcontroller-spring-framework-20180115t104630305z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-15 10:46:24
last_update2018-01-15 10:46:24
depth2
children0
last_payout2018-01-22 10:46:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length72
author_reputation1,501,149,936,516
root_title"How to develop Rest Web Service using @RestController - Spring Framework"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id29,673,535
net_rshares0