Spring and RESTful webservice Integration
Earlier, we had developed an application using Spring and Rest. But, this is a basic app which shows integration of Spring with Rest at a beginner level. This demo app is for freshers.
Following below steps to create this application:
1) Libraries required:
spring-core 4.1.6 RELEASE
spring-web 4.1.6 RELEASE
spring-webmvc 4.1.6 RELEASE
jstl 1.2
2) Create a xml file named as web.xml in WEB-INF folder and write following code:
<!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>springrest</servlet-name>
  <servlet-class>
   org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>springrest</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>
3) Create a xml file named as springrest-servlet.xml in WEB-INF folder and write following code in it:
<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="com.gag.controller" />
</beans>
4) Create a package named as com.gag.controller and Create a class named as SpringRestController.java and write following code in it:
package com.gag.controller;
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
@RequestMapping("/hello")
public class SpringRestController {
 @RequestMapping(value = "/{name}", method = RequestMethod.GET)
 public String hello(@PathVariable String name) {
  String result = "Hello " + name;
  return result;
 }
}
5) Create a JSP or any html file and write following code in it:
<html>
<body>
<a href="hello/Gagan">Say Hello</a>
</body>
</html>
Our output would be like as below:
This is an anchor tag on which you would click and a Hello Message is shown:
Nice piece of information allocated by you. Continue updating this praiseworthy write-ups.
ReplyDeleteWebsite Designer in Lucknow | Web design company