Issue
Looking for some guidance on converting below in existing applicationContext.xml into Java Config for a new Springboot application.
<bean id="InitialHandler" class="package.InitialHandler"
scope="prototype">
<property name="rawResponseTemplate"
ref="rawResponseRestTemplate"/>
<property name="mimeMapper" ref="initialMimeMapper"/>
</bean>
<bean id="rawResponseRestTemplate"
class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="package.CommonDocumentRawMessageConverter"/>
</list>
</property>
</bean>
<bean id="initialMimeMapper"
class="package.ConfigurationBasedMapper">
<property name="configuration" ref="initialMimeConfig"/>
</bean>
<bean id="initialMimeConfig"
class="org.apache.commons.configuration.XMLPropertiesConfiguration"
scope="singleton">
<constructor-arg type="java.net.URL"
value="classpath:FileINeedLoaded.xml"/>
<property name="reloadingStrategy" ref="ReloadingStrategy"/>
</bean>
// snippet of CommonDocumentRawMessageConverter below
import org.springframework.http.converter.HttpMessageConverter;
public class CommonDocumentRawMessageConverter implements
HttpMessageConverter<CommonDocument> {}
This is what i have:
@Bean
public RestTemplate rawResponseRestTemplate() {
RestTemplate rawResponseRestTemplate = new RestTemplate();
rawResponseRestTemplate.setMessageConverters(msgConverters());
return rawResponseRestTemplate;
}
private List<HttpMessageConverter<?>> msgConverters() {
List<HttpMessageConverter<?>> newList = new ArrayList<HttpMessageConverter<?>>();
newList.add(new CommonDocumentRawMessageConverter());
return newList;
}
Service compiles OK but get runtime exception as follows:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'prop.mime-mapper' to
package.util.Mapper<java.lang.String>:
Property: prop.mime-mapper
Value: propMimeMapper
Origin: "prop.mimeMapper" from property source "Config resource 'class
path resource [application.yml]' via location 'optional:classpath:/'
(document #0)"
Reason: org.springframework.core.convert.ConverterNotFoundException: No
converter found capable of converting from type [java.lang.String] to
type [package.util.Mapper<java.lang.String>]
Action:
Update your application's configuration
Snippet of my config:
@Getter
@Setter
public class propConfig {
private String randomProp;
private Mapper<String> mimeMapper;
private RestTemplate rawResponseTemplate;
Service:
@Slf4j
@Service
public class PropService extends DocumentManagementService {
private final RestTemplate rawResponseTemplate;
private final Mapper<String> mimeMapper;
public PropService(@Autowired PropConfig propConfig) {
this.rawResponseTemplate = propConfig.getRawResponseTemplate();
this.mimeMapper = propConfig.getMimeMapper();
}
It appears that at runtime the properties defined as RestTemplate and Mapper are not getting converted for some reason.
UPDATE ONE I was able to fix above issue by removing these properties from the application.yaml altogether.
private Mapper<String> mimeMapper;
private RestTemplate rawResponseTemplate;
I realised that these properties were actually 'refs' in the original applicationContext.xml and thus not required in the new application.yaml. I do have new issues however related to this conversion which i will share. I will keep original configs in place in case others have same issue.
Solution
I was able to fix above issue by removing these properties from the application.yaml altogether.
private Mapper<String> mimeMapper;
private RestTemplate rawResponseTemplate;
I realised that these properties were actually 'refs' in the original applicationContext.xml and thus not required in the new application.yaml.
Answered By - user1746582
Answer Checked By - Candace Johnson (JavaFixing Volunteer)