Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将基于SpringXML的配置转换为基于Java的配置_Java_Xml_Spring_Configuration_Spring Annotations - Fatal编程技术网

将基于SpringXML的配置转换为基于Java的配置

将基于SpringXML的配置转换为基于Java的配置,java,xml,spring,configuration,spring-annotations,Java,Xml,Spring,Configuration,Spring Annotations,我尽量不使用任何xml <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.xml.MarshallingHttpMessa

我尽量不使用任何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
公共RestTemplate RestTemplate(){
RestTemplate RestTemplate=新RestTemplate();
列表>();
add(marshallingMessageConverter());
restTemplate.setMessageConverters(转换器);
返回REST模板;
}
这里有个问题

<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
尝试将“com.cloudlb.domain.User”转换为类[]无法工作

@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);
}
@Bean
公共MarshallingHttpMessageConverter marshallingMessageConverter(){
Jaxb2Marshaller-marshaller=新的Jaxb2Marshaller();
//
列表>();
添加(User.class);
marshaller.setClassesToBeBound((Class[])listClass.toArray();
// --------------------------------
返回新的MarshallingHttpMessageConverter(marshaller,marshaller);
}
错误:铸造有问题


提前谢谢。

setClassesToBeBound
需要一个vararg列表,因此您可以执行以下操作:

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

发布您收到的错误不应将
转换为
列表而不是数组?@Thomas:
将强制转换为任何必要的内容,例如
列表或数组。
@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;
}