Announcement Announcement Module
Collapse
No announcement yet.
Rest web service - xml view not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rest web service - xml view not working

    Hi all

    I am new and writing a basic REST web service with Spring WS and using JAXB2 for XML view.
    In my servlet xml i defined my XML view & also defined contentNegotiatingViewResolver (i dont actually need it)

    Code:
     
    
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
          <property name="messageConverters">
          <list>
            <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
              <property name="unmarshaller" ref="jaxbMarshaller"/>
              <property name="marshaller" ref="jaxbMarshaller"/>
            </bean>
          </list>
        </property> 
      	</bean>
      		
    
      
    	<!--Use JAXB OXM marshaller to marshall/unmarshall following class-->
    	 <bean id="jaxbMarshaller" 	class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    	<property name="classesToBeBound">
    	<list>
    	    <value>com.bti.pojos.myPojo</value>
    	    
    	</list>
    	</property>
    	</bean>
    
    	
     	 <!--<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />-->
    	<!--  XML view for marshalling/unmarshalling-->
    
    	<bean id="XMLView" class="org.springframework.web.servlet.view.xml.MarshallingView">
    	<property name="contentType" value="application/xml;charset=UTF-8"/>
    	<constructor-arg ref="jaxbMarshaller"/>
    	</bean>
    	
    	
    	<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    	<property name="mediaTypes">
    	<map>
    		<entry key="xml" value="application/xml"/>
    		<entry key="html" value="text/html"/>
    	</map>
    	</property>
    	<property name="viewResolvers">
    	<list>
    		<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
    		<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    		<property name="prefix" value="/WEB-INF/jsp/"/>
    		<property name="suffix" value=".jsp"/>
    	</bean>
    	</list>
    </property>
    From my controller, i have some handler methods and i realized only 2nd way works but i wonder why.
    I followed the tutorial & books which define 2nd method as correct.
    Please suggest what do i miss in settings.
    Code:
    1.          public myPojo handleMethod1(@PathVariable("id") String id) {
    
    
    		.....
                                return myPojoObj;
    		
    	} //only this works, why is that so?
    
    2.
    public String handleMethod2(@PathVariable("id") String id,Model model) {
    
                              .........
                            model.addAttribute("myPojo", myPojoObj);
    		return "XMLView";
    		
    	} //prints  string XMLView instead of  resolving view & model data -wrong
    
    
    3.public ModelAndView Pojo handleMethod3(@PathVariable("id") String id) {
    
    
    		.........
                                return new ModelAndView("XMLView",myPojoObj,"myPojo",);
                                   //myPojo is XmlRootElement 
                                 //on Pojo class
    		
    	}// thiswont work at all - no output
Working...
X