6

I try not to using any xml.

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

    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                <property name="marshaller" ref="jaxbMarshaller"/>
                <property name="unmarshaller" ref="jaxbMarshaller"/>
            </bean>
            <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
        </list>
    </property>
</bean>

like this one: convert to @Bean

@Bean
public RestTemplate restTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();

    converters.add(marshallingMessageConverter());
    restTemplate.setMessageConverters(converters);

    return restTemplate;
}

Problem here.

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.cloudlb.domain.User</value>
        </list>
    </property>
</bean>

Try to convert "com.cloudlb.domain.User" into Class [] not thing work.

@Bean
public MarshallingHttpMessageConverter marshallingMessageConverter() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();

    //
    List<Class<?>> listClass = new ArrayList<Class<?>>();
    listClass.add(User.class);

    marshaller.setClassesToBeBound((Class<?>[])listClass.toArray());
    // --------------------------------

    return new MarshallingHttpMessageConverter(marshaller, marshaller);
}

Error: problem with casting.

Thank you in advance.

3
  • post the error you are getting Commented Jan 3, 2012 at 20:09
  • Shouldn't <list> be converted to a List rather than an array? Commented Jan 3, 2012 at 20:12
  • @Thomas: <list> will be coerced to whatever is necessary, e.g. List, or array. Commented Jan 3, 2012 at 20:16

2 Answers 2

12
@Bean
public MarshallingHttpMessageConverter marshallingMessageConverter() {
    return new MarshallingHttpMessageConverter(
        jaxb2Marshaller(),
        jaxb2Marshaller()
    );
}

@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(new Class[]{
               twitter.model.Statuses.class
    });
    return marshaller;
}
Sign up to request clarification or add additional context in comments.

Comments

4

setClassesToBeBound takes a vararg list, so you can just do this:

Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(User.class);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.