 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> ```  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> ```  - 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; } } ```  ### Result - Lets run the project using tomcat server call the controller using ARC Client  - run on web browser  - parse retuned JSON data with json parser  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/>
author | ravik2492 | ||||||
---|---|---|---|---|---|---|---|
permlink | how-to-develop-rest-web-service-using-restcontroller-spring-framework | ||||||
category | utopian-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}} | ||||||
created | 2018-01-15 10:42:45 | ||||||
last_update | 2018-01-16 14:08:21 | ||||||
depth | 0 | ||||||
children | 3 | ||||||
last_payout | 2018-01-22 10:42:45 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 0.043 HBD | ||||||
curator_payout_value | 0.000 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 6,922 | ||||||
author_reputation | 1,501,149,936,516 | ||||||
root_title | "How to develop Rest Web Service using @RestController - Spring Framework" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 29,672,914 | ||||||
net_rshares | 6,464,489,218 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
ravik2492 | 0 | 2,210,119,362 | 100% | ||
tripathiji | 0 | 596,434,260 | 100% | ||
bhuvn | 0 | 593,781,840 | 100% | ||
riaz | 0 | 582,684,647 | 100% | ||
andreisem | 0 | 51,584,384 | 100% | ||
vickyz | 0 | 615,584,262 | 100% | ||
mvanyi | 0 | 532,733,164 | 100% | ||
ninelsaveleva | 0 | 52,207,299 | 100% | ||
rolofnals | 0 | 614,860,000 | 100% | ||
vzdanova | 0 | 614,500,000 | 100% |
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)**
author | manishmike10 |
---|---|
permlink | re-ravik2492-how-to-develop-rest-web-service-using-restcontroller-spring-framework-20180116t141233265z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"} |
created | 2018-01-16 14:12:36 |
last_update | 2018-01-16 14:12:36 |
depth | 1 |
children | 0 |
last_payout | 2018-01-23 14:12:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 285 |
author_reputation | 20,399,732,899,016 |
root_title | "How to develop Rest Web Service using @RestController - Spring Framework" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,949,148 |
net_rshares | 0 |
Great work. Make a complete tutorial
author | neelamgahlyan |
---|---|
permlink | re-ravik2492-how-to-develop-rest-web-service-using-restcontroller-spring-framework-20180115t104414760z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"app":"steemit/0.1"} |
created | 2018-01-15 10:44:15 |
last_update | 2018-01-15 10:44:15 |
depth | 1 |
children | 1 |
last_payout | 2018-01-22 10:44:15 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 36 |
author_reputation | 4,455,241,403,180 |
root_title | "How to develop Rest Web Service using @RestController - Spring Framework" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,673,173 |
net_rshares | 0 |
Thank you ! 2 hours to complete the work and half hour to write tutorial
author | ravik2492 |
---|---|
permlink | re-neelamgahlyan-re-ravik2492-how-to-develop-rest-web-service-using-restcontroller-spring-framework-20180115t104630305z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"app":"steemit/0.1"} |
created | 2018-01-15 10:46:24 |
last_update | 2018-01-15 10:46:24 |
depth | 2 |
children | 0 |
last_payout | 2018-01-22 10:46:24 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 72 |
author_reputation | 1,501,149,936,516 |
root_title | "How to develop Rest Web Service using @RestController - Spring Framework" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,673,535 |
net_rshares | 0 |