Friday, 3 February 2017

Spring rest example


  1. Start First Spring REST Example
  2. Requirement For Spring REST First Example 
  3. Technologies Used 
                          Eclipse  IDE
                          JRE 1.7
                          Apache Tomcat 7
                          Spring 4.0.0

Spring REST Application Structure

               

1 SpringServiceController.java File

package com.jeetdevelop.springrest.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("/firstRest/application")
public class SpringServiceController
{
@RequestMapping(value = "/{name}", method = RequestMethod.GET,headers="Accept=application/json")
public Result getGreeting(@PathVariable String name)
{
return new Result(name,"Jangir");
}
}

2.Result.java File
package com.jeetdevelop.springrest.controller;
 public class Result
{
private String fname;
private String lname;

public String getFname()
{
return fname;
}

public String getLname()
{
return lname;
}

public Result(String fname, String lname)
{
this.fname = fname;
this.lname = lname;
}
}

3. Rest-servlet.xml File
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
<context:component-scan base-package="com.jeetdevelop.springrest.controller" />
<mvc:annotation-driven />
  </beans>


4. Web.xml File

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
   <display-name>JeetSpringRest</display-name>
   <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
   </welcome-file-list>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

Apply Following File and Build your application Than Run On  Server Your Application
and see output 
BaseURL=localhost:8081/JeetSpringRest/
API URL=firstRest/application/Jeet
Jeet is a input paramerts

Ask Question Please Commets :
Thanks

No comments:

Post a Comment