[IMAGE: https://www.javatpoint.com/images/spimages/spring1.png]
SourceHere
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
- configuring web.xml file
First of all paste all jar files into lib folder
Here we configure web.xml file to run application application
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/config/spring-servlet.xml
1
spring
/
[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012856/sl4tmx5pf01oo2jkqofb.png]
- configuring spring-servlet.xml
Basic configuration of databse, view resolver and sessionfactory
```
com.blog.model.Users
${hibernate.dialect}
${hibernate.show_sql}
${hibernate.hbm2ddl.auto}
[IMAGE: 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 list = listofStudents();
String json = new ObjectMapper().writeValueAsString(list);
return json;
}
List list = new ArrayList();
public List 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;
}
}
[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012892/bhzpvotg0rsp48hef9sv.png]
Result
-
Lets run the project using tomcat server call the controller using ARC Client
[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012493/gazows1dozstbkjuewtv.png] -
run on web browser
[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012563/lfotzpxpatt59jyv3xqf.png] -
parse retuned JSON data with json parser
[IMAGE: 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
Posted on Utopian.io - Rewarding Open Source Contributors