我们有多个RESTful服务,需要在JAXB和域对象之间进行对象映射,反之亦然。
我正在考虑定义一个类似于以下内容的通用界面。 // mapper interface
public interface Mapper{
Object toDomainObject(Object jaxb);
Object toClientObject(Object domain);
}
// RecorderMapper类 - 实现Mapper接口
public class RecorderMapper implements Mapper{
@Override
public Object toDomainObject(Object obj) {
JAXBRecorderRequest jAXBRecorderRequest = (JAXBRecorderRequest)obj;
RecorderDomain recorderDomain = new RecorderDomain();
//map to jaxb to domain
return recorderDomain;
}
@Override
public Object toClientObject(Object obj) {
RecorderDomain recorderDomain = (RecorderDomain)obj;
JAXBRecorderResponse jaxbRecorderResponse = new JAXBRecorderResponse();
//map domain to jaxb
return jaxbRecorderResponse;
}
}
// Recorder服务类 - 使用Mapper进行对象映射
public class Recorder Service {
@Autowired
private Mapper mapper;
public JAXBRecorderResponse create(JAXBRecorderRequest jaxbRecorderRequest){
RecorderDomain recorderDomain =(RecorderDomain) mapper.toDomainObject(jaxbRequest);
return (JAXBRecorderResponse)mapper.toClientObject(recorderDomain);
}
}
主要思想是定义一个通用模式来实现整个团队的对象映射。
我想知道是否有更好的方法。
非常感谢任何帮助。