2009-11-06 9 views
9

Sto usando il framework RC1 Spring 3.0 e sto testando il mvc Spring. Volevo usare Spring mvc per gestire richieste riposanti. Ho configurato il mio controller per gestire la richiesta URI. Sto passando in xml con la richiesta. Così sul controller Ho un metodo come segue:Come convertire XML in un oggetto utilizzando il mvc Spring 3.0 mentre faccio richiesta RESTful

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

Sto avendo difficoltà a convertire il codice XML al RequestObject. Non ho visto molta documentazione su questo e mi chiedevo se qualcuno potesse indicarmi la giusta direzione. Immagino che dovresti annotare il RequestObject usando JAXB o qualcosa del genere per dire a Spring di convertire il file xml in RequestObject ma non ne sono sicuro.

Grazie per tutto il vostro aiuto !!

risposta

8

Per convertire XML in oggetto Java è possibile utilizzare Apache Digest http://commons.apache.org/digester/. Spring lo usa da solo internamente.

Aggiornamento Non ero a conoscenza di questa nuova funzionalità in Spring 3.0. Mi dispiace per averti maltrattato. Ho scritto un test rapido e questo è quello che dovresti fare.

1) Impostare ViewResoler e MessageConverter in -servlet.xml. Nel mio test sembra che questo

<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) Aggiungi annotazioni struttura XML nella vostra classe Java


@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) Aggiungere la mappatura di annotazione nella vostra classe Controller come


@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); 
    }  
} 

Spero che questo ti aiuterò.

+0

Grazie di aver ricevuto il collegamento .... ma forse non stavo cercando una libreria che convertisse xml. Mi stavo chiedendo come Spring mvc che l'xml deve essere convertito in un oggetto. L'oggetto è nullo, non importa quale I POST nel corpo della richiesta. – brock

+0

Questo è esattamente quello che stavo cercando. Suppongo che mi mancasse il messaggio Convertitore. Ho visto la documentazione su @RequestBody ma non ero sicuro se questo è quello per cui dovrebbe essere usato. Grazie per l'aiuto!! – brock

Problemi correlati