복붙노트

[SPRING] Spring XML 기반에서 Java 기반 구성으로 변환

SPRING

Spring XML 기반에서 Java 기반 구성으로 변환

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>

이 것을 좋아해요. @Bean으로 변환하십시오.

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

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

    return restTemplate;
}

여기 문제가있어.

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

"com.cloudlb.domain.User"를 Class []가 아닌 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);
}

오류 : 전송에 문제가 있습니다.

미리 감사드립니다.

해결법

  1. ==============================

    1.

    @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;
    }
    
  2. ==============================

    2.setClassesToBeBound는 vararg 목록을 취하므로 다음과 같이하면됩니다.

    setClassesToBeBound는 vararg 목록을 취하므로 다음과 같이하면됩니다.

    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(User.class);
    
  3. from https://stackoverflow.com/questions/8717992/convert-spring-xml-based-to-java-based-configuration by cc-by-sa and MIT license