SolveItproblems

SolveITProblems is a project of QFormat.net intellectual club

SolveITProblems это проект интеллектуального клуба QFormat.net

воскресенье, 24 марта 2013 г.

Maven + Spring MVC + RestFull WS. Step by Step

If you read our first tutorial about basic SpringMVC web-app you need to make only 4 steps.
Lets begin:

Step 1.
Create a class that will be responsed to our clients as XML or JSON.






 package com.bitep.mavenspring;  
 import javax.xml.bind.annotation.XmlRootElement;  
 /**  
  *  
  * @author vusal  
  */  
 @XmlRootElement(name="marshaller")  
 public class Ent {  
   int id;  
   String name;  
   String password;  
   public Ent(int id, String name, String password) {  
     this.id = id;  
     this.name = name;  
     this.password = password;  
   }  
   public Ent() {  
   }  
   public int getId() {  
     return id;  
   }  
   public void setId(int id) {  
     this.id = id;  
   }  
   public String getName() {  
     return name;  
   }  
   public void setName(String name) {  
     this.name = name;  
   }  
   public String getPassword() {  
     return password;  
   }  
   public void setPassword(String password) {  
     this.password = password;  
   }  
 }  

Step 2.
Create a new controller for restfull requests:

 package com.bitep.mavenspring;  
 import org.springframework.stereotype.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.servlet.ModelAndView;  
 /**  
  *  
  * @author vusal  
  */  
 @Controller  
 public class RestController {  
   @RequestMapping(method= RequestMethod.GET, value = "rest/{id}")  
   public ModelAndView getEnt(@PathVariable String id)  
   {         
     Ent e = new Ent(id,"test","test1");
     return new ModelAndView("marshaller", "object", e);      
   }  
 }  

Step 3.
New dependency a POM.xml and a couple of lines at web.xml:

  <dependency>  
       <groupId>org.springframework.ws</groupId>  
       <artifactId>spring-ws-core</artifactId>  
       <version>2.1.2.RELEASE</version>  
     </dependency>  


   <!-- For Rest-->  
   <context-param>  
 <param-name>contextConfigLocation</param-name>  
 <param-value>/WEB-INF/springMVC-servlet.xml</param-value>  
 </context-param>  
 <listener>  
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
 </listener>  


Step 4.
Changes at springMVC-servlet.xml:

 <?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"  
      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">  
 <context:component-scan base-package="com.bitep.mavenspring" />  
 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  

 <bean id="jaxbMarshaller"  
 class="org.springframework.oxm.jaxb.Jaxb2Marshaller">  
 <property name="classesToBeBound">  
      <list>  
        <value>com.bitep.mavenspring.Ent</value>         
      </list>  
 </property>  
 </bean>  
 <bean id="marshaller" class="org.springframework.web.servlet.view.xml.MarshallingView">  
 <constructor-arg ref="jaxbMarshaller" />  
 </bean>  
 <bean id="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" />  
   
 </beans>  

And RUN it :)

Many things here needs a description. This article is not intended to teach SpringMVC or Restfull web-services. Its just a guide for developers who knows the basics and want to look at the config files or just to overwiew the theme. If you have questions feel free to ask.


Комментариев нет:

Отправить комментарий