我有以下方法提供肥皂服务:
@Override
public SomeResponse someRequest(SomeRequest request) {
org.w3c.dom.Element any = request.getMessagePrimaryContent().getAny();
}
any-可以包含任何“内部请求”。我知道所有类型。例如:ImportBooksRequest, ImportCDRequest, ImportSongsRequest
并且我可以将任何一个解编为具体的类:
if (any.getLocalName().equals(ImportBooksRequest.class.getSimpleName())) {
ImportBooksRequest importBooksRequest = marshaler.unMarshal(any, ImportBooksRequest.class);
} else if(any.getLocalName().equals(ImportCDRequest.class.getSimpleName())){
ImportCDRequest importCDRequest = marshaler.unMarshal(any, ImportBooksRequest.class);
} else if(any.getLocalName().equals(ImportSongsRequest.class.getSimpleName())){
ImportSongsRequest importSongsRequest= marshaler.unMarshal(any, ImportSongsRequest.class);
}
然后,我需要将请求重定向到具体请求。现在我使用处理程序:
someRequestHandlersResolver.getSomeRequestHandler(any.getLocalName()).process(request);
在someRequestHandlersResolver
中,我为每个concreate请求提供了hadler
@Component
public class SomeRequestHandlersResolverImpl implements SomeRequestHandlersResolver {
private Map<String, SomeRequestHandler> map = new HashMap<>();
public SomeRequestHandlersResolverImpl(ImportBooksRequest importBooksRequest, ImportCDRequest importCDRequest, ImportSongsRequest importSongsRequest) {
map.put(ImportBooksRequest.class.getSimpleName(), importBooksRequest);
map.put(ImportCDRequest.class.getSimpleName(), importCDRequest);
map.put(ImportSongsRequest.class.getSimpleName(), importSongsRequest);
}
@Override
public SomeRequestHandler getSomeRequestHandler(String type) {
SomeRequestHandler someRequestHandler = map.get(type);
if (someRequestHandler == null)
throw new IllegalArgumentException("Invalid SomeRequest type: " + type);
return someRequestHandler;
}
}
我不确定这是正确的。也许还有其他方式或方法可以实现这一目标?