SolveItproblems

SolveITProblems is a project of QFormat.net intellectual club

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

понедельник, 8 апреля 2013 г.

Maven + Spring MVC + Multiple View (json, xml and ftl)

3 Big steps:

Step 1 - Prepare dependency:

    <dependency>  
       <groupId>org.codehaus.jackson</groupId>  
       <artifactId>jackson-mapper-asl</artifactId>  
       <version>1.7.1</version>  
     </dependency>  
      <dependency>   
       <groupId>org.springframework.ws</groupId>   
       <artifactId>spring-ws-core</artifactId>   
       <version>2.1.2.RELEASE</version>   
     </dependency>   



Step 2 - Prepare Java classes:

 /**  
  *  
  * @author Vusal J. Khalilov  
  */  
 @Controller  
 public class LoginController {  
   @RequestMapping(method= RequestMethod.GET,value="/login")  
   public ModelAndView login()  
   {  
     return new ModelAndView("login", "login", new LoginResponse(true));      
   }  
 }  

 package com.azercell.web.restvcc.response;  
 import javax.xml.bind.annotation.XmlRootElement;  
 /**  
  *  
  * @author Vusal J. Khalilov  
  */  
 @XmlRootElement(name="login")  
 public class LoginResponse{  
   private boolean isLoggedIn;  
   public LoginResponse(boolean isLoggedIn) {  
     this.isLoggedIn = isLoggedIn;  
   }  
   public LoginResponse() {  
   }  
   public boolean isIsLoggedIn() {  
     return isLoggedIn;  
   }  
   public void setIsLoggedIn(boolean isLoggedIn) {  
     this.isLoggedIn = isLoggedIn;  
   }  
 }            

Step 3 - Prepare spring controller (the main part):

    <context:annotation-config/>  
   <mvc:annotation-driven />  
   <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">  
     <property name="classesToBeBound">  
       <list>  
         <value>com.azercell.web.restvcc.response.LoginResponse</value>          
       </list>  
     </property>  
   </bean>  
   <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">  
     <property name="order" value="1" />  
     <property name="mediaTypes">  
       <map>  
         <entry key="json" value="application/json" />  
         <entry key="xml" value="application/xml"/>          
       </map>  
     </property>  
     <property name="defaultViews">  
       <list>  
          <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />  
         <bean class="org.springframework.web.servlet.view.xml.MarshallingView">  
           <constructor-arg ref="jaxbMarshaller" />  
         </bean>  
       </list>  
     </property>  
   </bean>  
         <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">            
           <property name="order" value="2"/>  
           <property name="cache" value="true"/>  
           <property name="prefix" value=""/>  
           <property name="suffix" value=".ftl"/>  
         </bean>  

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

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