Announcement Announcement Module
No announcement yet.
Rest web service - xml view not working Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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)

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
          <property name="messageConverters">
            <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
              <property name="unmarshaller" ref="jaxbMarshaller"/>
              <property name="marshaller" ref="jaxbMarshaller"/>
    	<!--Use JAXB OXM marshaller to marshall/unmarshall following class-->
    	 <bean id="jaxbMarshaller" 	class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    	<property name="classesToBeBound">
     	 <!--<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 class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    	<property name="mediaTypes">
    		<entry key="xml" value="application/xml"/>
    		<entry key="html" value="text/html"/>
    	<property name="viewResolvers">
    		<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"/>
    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.
    1.          public myPojo handleMethod1(@PathVariable("id") String id) {
                                return myPojoObj;
    	} //only this works, why is that so?
    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