Spring自定义通用转换器无法正常工作

时间:2017-12-01 16:00:04

标签: spring-mvc converter

在我的spring mvc web-application中,我使用通用转换器,通过使用(service和dao)组件获取将String(id)转换为Company

首先在我的MVC-config中添加转换器如下:

@Override
public void addFormatters(FormatterRegistry registry) {
    registry.addConverter(new GenericIdToCompanyConverter(new CompanyServiceImp()));
}

companyService

@Service
@Transactional
@Qualifier("companyService")
public class CompanyServiceImp implements ICompanyService {

    @Resource
    @Qualifier("companyDAO")
    private ICompanyDao dao;


    public void setDao(ICompanyDao dao) {
        this.dao = dao;
    }

    @Override
    public Company find(Long id) throws BusinessException {
        Company current = dao.find(id);
        if(current == null) {
            throw new BusinessException("notFound");
        }
        return current;
    }
....
}

通用转换器:

public class GenericIdToCompanyConverter implements GenericConverter {

    private ICompanyService companyService;

    public GenericIdToCompanyConverter(ICompanyService companyService) {
        super();
        this.companyService = companyService;
    }

    @Override
    public Set<ConvertiblePair> getConvertibleTypes() {
        ConvertiblePair[] pairs = new ConvertiblePair[] { new ConvertiblePair(Number.class, Company.class), new ConvertiblePair(String.class, Company.class) };
        return ImmutableSet.copyOf(pairs);
    }

    @Override
    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
        long id = 0;
        if( sourceType.getType() == String.class) {
            try {
                id = Long.valueOf((String) source);
            }catch(NumberFormatException e) {
                return null;
            }
        }else if( sourceType.getType() == Number.class) {
            id = (Long) source;
        }else {
            return null;
        }

        try {
            return companyService.find(Long.valueOf(id));
        } catch (BusinessException e) {
            return null;
        }
    }
}

这里是接收数据形式的控制器(通过ajax请求)

public @ResponseBody JsonResponseBean applay(@Valid VoucherForm form, BindingResult result, Locale locale) throws BusinessException {
....
}

其中VoucherForm具有这些属性

public class VoucherForm{
    protected Long id;
    protected Company company;
...
}

当我运行应用程序并调用控制器方法时,它返回公司属性的类型不匹配错误 当我在调试模式下执行此操作时,我看到它在serviceCompany上失败 - dao.find(id)statment其中我的dao == null

请帮忙

1 个答案:

答案 0 :(得分:0)

最后我必须自动装配转换器

MVC-配置 ....

@Autowired
private GenericIdToCompanyConverter genericIdToCompanyConverter;
@Override
public void addFormatters(FormatterRegistry registry) {
    registry.addConverter(genericIdToCompanyConverter);
}

并更新转换器,如下所示:

public class GenericIdToCompanyConverter implements GenericConverter {

    @Resource
    @Qualifier("companyService")
    private ICompanyService companyService;

    @Override
    public Set<ConvertiblePair> getConvertibleTypes() {
        ....
    }

    @Override
    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
       ....
    }
}