道歉,如果已经被问过/回答过一千次(我先检查过)。
我不是一个Java程序员,而是负责扩展现有的Java SOAP服务。我很想避免复制/粘贴我知道有效的现有代码,但是想知道java中可用的最佳选项是什么。
基本上我已经有了这个方法:
public String methodThatIDontWantToCopyPaste(
@WebParam(name = "a", partName = "a") String paramA,
@WebParam(name = "b", partName = "b") int paramB,
@WebParam(name = "c", partName = "c") int paramC) {
// Validate parameters
if (paramA.isEmpty() ||
0 == paramB ||
0 == paramC) {
return "Invalid request";
}
String response = "";
try {
// Parmaeters OK, build client
/*
lots of generic implementation here
...
XYZ client = ...
...
*/
response = client.specificMethodToHandleABC(paramA, paramB, paramC);
} catch (SOAPException | IOException ex) {
// handling omitted
}
return response;
}
我想添加几个新的/类似的方法,但每个方法都有:
我通常会使用我常用的编程语言进行回调,但它是一种专有语言,并不像Java那么强大,所以我想知道最佳选择是什么?
答案 0 :(得分:0)
在类似的设置中,我通过调用每个端点的方法并将每个变量部分的回调传递给方法来使用回调/匿名类,如下所示
public String methodA(final int param1, final String param2) throws Exception {
return this.call(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return param1 != 0 && param2 != null;
}
});
}
public String methodB(final String param1, final String param2) throws Exception {
return this.call(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return param1 != null && param2 != null;
}
});
}
private String call(Callable<Boolean> validationCallable) throws Exception {
// static code similar to all methods
assert validationCallable.call().equals(Boolean.TRUE);
// static code similar to all methods
return ""; // probably result based on another callable
}