views:

1105

answers:

1

Hi, I'm using the Spring 3.0 RC1 framework and I'm currently testing out Spring mvc. I wanted to use Spring mvc to handle restful requests. I have set up my controller to handle the URI request. I am passing in xml with the request. So on the controller I have a method like follows:

public void request(RequestObject request) {
  doSomething();
}

I am having a hard time converting the xml to the RequestObject. I haven't seen much documentation on this and I was wondering if anyone could point me in the right direction. I'm guess that you would have to annotate the RequestObject using JAXB or something in order to tell Spring to convert the xml file to RequestObject but I'm not sure.

Thanks for all of your help!!

+2  A: 

For converting XML to Java object you can use Apache Digest http://commons.apache.org/digester/. Spring uses it itself internally.

Update I wasn't aware about this new feature in Spring 3.0. Sorry for misdealing you. I wrote quick test and this is what you should do.

1) Set up ViewResoler and MessageConverter in -servlet.xml. In my test it looks like this

 <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

 <bean id="person" class="org.springframework.web.servlet.view.xml.MarshallingView">
  <property name="contentType" value="application/xml"/>
  <property name="marshaller" ref="marshaller"/>
 </bean>

 <oxm:jaxb2-marshaller id="marshaller">
  <oxm:class-to-be-bound name="com.solotionsspring.test.rest.model.Person"/>
 </oxm:jaxb2-marshaller>

 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
     <property name="messageConverters">
       <list>
         <ref bean="marshallingHttpMessageConverter"/>
       </list>
     </property>
 </bean>

 <bean id="marshallingHttpMessageConverter" 
       class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
   <property name="marshaller" ref="marshaller" />
   <property name="unmarshaller" ref="marshaller" />
 </bean>

2) Add XML structure annotations into your Java class


@XmlRootElement
public class Person {
    private String name;
    private int age;
    private String address;
    /**
     * @return the name
     */
    @XmlElement
    public String getName() {
     return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
     this.name = name;
    }
    /**
     * @return the age
     */
    @XmlElement
    public int getAge() {
     return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(int age) {
     this.age = age;
    }
    /**
     * @return the address
     */
    @XmlElement
    public String getAddress() {
     return address;
    }
    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
     this.address = address;
    }
}

3) Add mapping annotation into your Controller class like


@Controller
public class RestController {

    @RequestMapping(value = "/person", method = RequestMethod.PUT)
    public ModelMap addPerson(@RequestBody Person newPerson) {
        System.out.println("new person: " + newPerson);
        return new ModelMap(newPerson);
    }    
}

Hope this will help you.

Jeka
Thank you got the link....but maybe I wasn't looking for a library that converted xml. I was wondering how to Spring mvc that the xml needs to be converted to an object. The object is null no matter what I POST in the request body.
brock
This is exactly what I was looking for. I guess I was missing the message Converter. I saw documentation on @RequestBody but wasn't sure if this is what it should be used for. Thank you for your help!!
brock