xyz*_*442 5 java xml configuration spring spring-annotations
我尽量不使用任何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>
Run Code Online (Sandbox Code Playgroud)
像这样:转换为@Bean
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(marshallingMessageConverter());
restTemplate.setMessageConverters(converters);
return restTemplate;
}
Run Code Online (Sandbox Code Playgroud)
问题在这里.
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.cloudlb.domain.User</value>
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
尝试将"com.cloudlb.domain.User"转换为Class []而不是工作.
@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);
}
Run Code Online (Sandbox Code Playgroud)
错误:投射问题.
先感谢您.
Tom*_*icz 11
@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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11258 次 |
| 最近记录: |