简体   繁体   中英

How to convert an xml config bean to java annotation bean ( spring boot )

I am new to java AOP. I am supposed to convert the following xml config to java annotation config in my spring boot application. May I know how exactly to convert this xml config to java annotation config:

I think none of the examples that I saw in stackoverflow match the pattern I am trying to convert.

    <bean id="xyzRestTemplate" 
          class="org.springframework.web.client.RestTemplate">
    <constructor-arg ref="xyzClientHttpRequestFactory" />
    <property name="messageConverters">
    <list>
        <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
 <property name="marshaller" ref="jaxbDataMarshaller" />
 <property name="unmarshaller" ref="jaxbDataMarshaller" />
       </bean>
  </list>
</property>
<property name="interceptors">
<list>
<bean class="com.example.XYZHeaderRequestInterceptor" />
</list>
    </property>
  </bean>
 <bean id="jaxbDataMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPaths">
   <list>
       <value>com.a.b.c.d.v2</value>
   </list>
 </property> </bean>

When people talk about converting from XML they don't mean necessarily doing the same thing exactly. What makes Spring Boot attractive isn't just that a configuration is a java class.

You should convert this to use RestTemplate https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-resttemplate.html

Then you just build the RestTemplate using the builder to have the JaxB marshaller and the interceptor you want.

Rest Template - XML Indentation

A nice testcase that passes with XML and passes with @Configuration classes will prove you got it right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM