具有参数化类型的Apache CXF

时间:2014-10-01 15:22:11

标签: java web-services cxf

我有一堆看起来一样的类,只包含一个Id和一些String属性。所以我尝试使用Generics来概括WebServices的创建:

@WebService
public interface IBasicCRUDService<E extends AbstractEntity, D extends AbstractEntityDTO, ID, DAO extends IGenericDAO<E, ID>>{

    public List<D> findAll(BasicFilters filters);
    public D findById(ID id);

    @WebMethod(exclude = true)
    public void setBaseDao(DAO dao);
}

实现:

public abstract class BasicCRUDService<E extends AbstractEntity, D extends AbstractEntityDTO, ID, DAO extends IGenericDAO<E, ID>> extends AbstractService implements IBasicCRUDService<E, D, ID, DAO> {

    private IGenericDAO<E, ID> dao;
    private Class<E> persistentClass;
    private Class<D> dataTransferClass;


    public final DAO getDao() {
        return (DAO) dao;
    }


    public void setDao(DAO dao) {
        this.dao = (IGenericDAO<E, ID>) dao;
    }


    @SuppressWarnings("unchecked")
    public Class<E> getPersistentClass() {
        if (persistentClass == null) {
            this.persistentClass = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        }
        return persistentClass;
    }

    @SuppressWarnings("unchecked")
    public Class<D> getDataTransferClass() {
        if(dataTransferClass == null) {
            this.dataTransferClass = (Class<D>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
        }
        return dataTransferClass;
    }


    @Override
    @Transactional(readOnly=true)
    @WebMethod(operationName="findAll")
    public List<D> findAll(BasicFilters filters) {
        List<E> pList = dao.findAll(filters);
        List<D> dList = new ArrayList<D>();
        for(E e:pList) {
            dList.add(this.map(e, getDataTransferClass()));
        }
        return dList;
    }

    @Override
    @Transactional(readOnly=true)
    @WebMethod(operationName="findAll")
    public D findById(ID id) {
        return this.map(dao.findById(id), getDataTransferClass());
    }

}

这将是一个具体的实现:     @Service(&#34; metodologiaService&#34)     公共类MetodologiaService扩展BasicCRUDService实现IMetodologiaService {

    @Override
    @Autowired
    @Qualifier("metodologiaDAO")
    public void setBaseDao(IMetodologiaDAO dao) {
        super.setDao(dao);
    }

}

@WebService
public interface IMetodologiaService extends IBasicCRUDService<Metodologia, MetodologiaDTO, Integer, IMetodologiaDAO>{
    public List<MetodologiaDTO> findAll(BasicFilters filters);
    public MetodologiaDTO findById(Integer id);
}

问题是,当这样做时,似乎CXF无法正确映射WebServices的属性。例如,在调用findById方法时,我得到了这个:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring>com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to java.lang.Integer</faultstring>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

但是,如果我直接在我的IMetodologiaService中声明方法,它可以工作:

@WebService
public interface IMetodologiaService extends IBasicCRUDService<Metodologia, MetodologiaDTO, Integer, IMetodologiaDAO>{
    public List<MetodologiaDTO> findAll(BasicFilters filters);
    public MetodologiaDTO findById(Integer id);
}

因此,在使用参数化类型时,似乎某种程度上扩展接口无效。有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

在这种情况下,您可能需要使用TypeTools之类的内容来解析类型参数:

public BasicCRUDService() {
  Class<?>[] typeArgs = TypeResolver.resolveRawArguments(IBasicCRUDService.class, getClass());
  persistentClass = typeArgs[0];
  dataTransferClass = typeArgs[1];
  dao = typeArgs[3];
}