storm_buster
storm_buster

Reputation: 7568

how to map xml to pojo class with rest template using spring 3?

i have too problems 1: debbuging restTemplate, 2: mapping xml to pojo.

here is my code Pojo:

@XmlRootElement(name = "parent")
public class Parent {

private User user;

public Parent(){        
}

//getter setter     
}

another Pojo

@XmlRootElement(name = "user")
public class User {

public User(){      
}
private long id;
    private String name;
    private Date registrationDate;  
}

I have an another webservice which return the xml data as :

<parent>
<user id="23" name="name">
<registrationdate>2012-02-27T13:08:31.771-05:00</registrationdate>
</user>
</parent>

I user Spring 3 and restemplate (in my classpath i have jaxb-api and jaxb-impl): in my appilacation-context i have :

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>

and in my service layer i have :

@Service
public class ParentServiceI implements ParentService{

Logger logger = Logger.getLogger(this.getClass());  

@Autowired
private RestTemplate restTemplate;

public Parent getMoreInfo() {
    logger.info("getting info");
    Parent p = restTemplate.getForObject("http://localhost:3128/dadad.xml", Parent.class);
    logger.info(p.toString());      
    return p;
}}

my first problem : When i started this code, i certainly had problem with the mapping, but i couldnt debug that, i wasnt able to see any error log, any exception , in my console i only get that :

09:31:50,503  INFO 959993440@qtp159257116-0 ParentServiceI :64 - getting info
09:31:50,670 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:78 - Created GET request for "http://localhost:3128/dadad.xml"
09:31:50,971 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:520 - Setting request Accept header to [application/xml, text/xml, application/*+xml, application/json]
09:31:58,762 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:465 - GET request for "http://localhost:3128/dadad.xml" resulted in 200 (OK)
09:31:58,764 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:78 - Reading [com.mypackage.Parent] as "text/xml" using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@7d6d4e3e]

And that it. no error, the code just stuck there. i get the first log message "getting info", i didnt get the 2nd log message how can i debug that?

my second problem : after setting the pojo right, i got some result :

09:31:50,503  INFO 959993440@qtp-159257116-0 ParentServiceI :64 - getting info
09:31:50,670 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:78 - Created GET request for "http://localhost:3128/dadad.xml"
09:31:50,971 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:520 - Setting request Accept header to [application/xml, text/xml, application/*+xml, application/json]
09:31:58,762 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:465 - GET request for "http://localhost:3128/dadad.xml" resulted in 200 (OK)
09:31:58,764 DEBUG 959993440@qtp-159257116-0 client.RestTemplate:78 - Reading [com.mypackage.Parent] as "text/xml" using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@7d6d4e3e]
09:31:59,337  INFO 959993440@qtp-159257116-0 serviceI.EquipmentServiceI:83 - Parent [user=User [id=0, name=null, registrationDate=Mon Feb 27 13:08:31 EST 2012]]

everthing is fine eccept the mapping? how can i fix that?

Thank you

Upvotes: 2

Views: 16975

Answers (2)

storm_buster
storm_buster

Reputation: 7568

i didnt find out how to resolve my first version.

to map that the attribute of xml file, i only need to add an annotation on the getter as

@XmlRootElement(name = "user")
public class User {

public User(){      
}
private long id;
    private String name;
    private Date registrationDate; 

@XmlAttribute(name="name")
    public String getName() {
        return name;
    }

}

Upvotes: 4

Waqas
Waqas

Reputation: 6802

In your application context file add following entry to use jaxb for marshalling/unmarshalling

<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>your.package.name.Parent</value>
            <value>your.package.name.User</value>
        </list>
    </property>
</bean>

Also you have to tell your rest template to use this marshaller by setting messageConverters property - like this [sample code copied from here]:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
    <list>
      <bean id="messageConverter"
            class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
        <property name="marshaller" ref="jaxb2Marshaller" />
        <property name="unmarshaller" ref="jaxb2Marshaller" />
      </bean>
    </list>
    </property>
</bean>

Upvotes: 2

Related Questions