SolveITProblems is a project of intellectual club

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

воскресенье, 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  
 public class Ent {  
   int id;  
   String name;  
   String password;  
   public Ent(int id, String name, String password) { = id; = name;  
     this.password = password;  
   public Ent() {  
   public int getId() {  
     return id;  
   public void setId(int id) { = id;  
   public String getName() {  
     return name;  
   public void setName(String 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  
 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:


   <!-- For Rest-->  

Step 4.
Changes at springMVC-servlet.xml:

 <?xml version="1.0" encoding="UTF-8"?>  
 <beans xmlns=""  
      xsi:schemaLocation=" ">  
 <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"  
 <property name="classesToBeBound">  
 <bean id="marshaller" class="org.springframework.web.servlet.view.xml.MarshallingView">  
 <constructor-arg ref="jaxbMarshaller" />  
 <bean id="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" />  

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.

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

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