为什么 DozerConverter 不工作?

why DozerConverter is not working?

我使用的是推土机版本 5.5.1。我想配置我的自定义转换器所以我有这个

import org.dozer.DozerConverter;

import com.example.movies.api.models.response.ClientResponseDTO;

public class MyCustomConverter
    extends DozerConverter<ClientResponseDTO, String> {

    public MyCustomConverter() {
        super(ClientResponseDTO.class, String.class);
    }

    @Override
    public String convertTo(ClientResponseDTO source, String destination) {
        return "ClientResponseDTO Converted to string!";
    }

    @Override
    public ClientResponseDTO convertFrom(String source, ClientResponseDTO destination) {
        return new ClientResponseDTO();
    }   

}

我用 Spring 加载的是这样的:

@Bean
public Mapper dozerBeanMapper() {
    DozerBeanMapper mapper = new DozerBeanMapper();
    List<CustomConverter> converters = new ArrayList<>();
    converters.add(new MyCustomConverter(ClientResponseDTO.class, String.class));
    mapper.setCustomConverters(converters);
    return mapper;
}

然后,我有这样的用法:

    @Autowired Mapper mapper;
    ...

    ClientResponseDTO clientResponseDTO = clientService.getClient(id);
    String conversion = this.mapper.map(clientResponseDTO, String.class);

自定义转换器从未被调用。你知道这是为什么吗?问候!

参考dozer documentation你应该添加到bean描述的映射。 例如

<bean id="org.dozer.Mapper" class="org.dozer.DozerBeanMapper">
<property name="mappingFiles">
<list>
  <value>systempropertymapping1.xml</value>
  <value>dozerBeanMapping.xml</value>
  <value>injectedCustomConverter.xml</value>
</list>
</property><property name="customConvertersWithId">
<map>
  <entry key="CustomConverterWithId" ref="configurableConverterBeanInstance1" />
  <entry key="CustomConverterWithId2" ref="configurableConverterBeanInstance2" />
</map>
</property>
</bean>