我有以下场景:我实现了一个运行在JBoss 5.1(带有Seam 2.2.0.GA)上的Java WS:
@Name("service")
@WebService(name = "Service", serviceName = "Service", targetNamespace = "http://app.service")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
@Stateless
public class Service implements ServiceContract {
@Override
@WebMethod(operationName = "serviceOperation")
public OperationResponse serviceOperation(@WebParam(name = "queryType") QueryType queryType) {
this.log.info(queryType);
// Validate queryType is not null:
if (queryType == null) {
return new OperationResponse("queryType is null");
}
// ... elided
return new OperationResponse("Query OK");
}
}
@XmlType
public enum QueryType {
LOCAL,
REMOTE;
}
@XmlType(name = "operationResponse", propOrder = {"message"})
public class OperationResponse {
private String message;
public OperationResponse () {
}
// getters and setters
}
Java客户端可以很好地使用它:
public class ServiceClient {
public void consume() {
OperationResponse response = svc.serviceOperation(QueryType.LOCAL);
this.log.info("rcop = #0", response.getMessage());
}
}
服务打印:
INFO [Service] LOCAL
客户端打印:
INFO [ServiceClient] Query OK
然而,如果从C#客户端(使用VS2008生成)消费,JavaWS将queryType作为空
INFO [Service]
即使设置了参数:
Service svc = new Service();
serviceOperation svcParams = new serviceOperation();
svcParams.queryType = queryType.LOCAL;
operationResponse response = svc.serviceOperation(svcParams);
Console.WriteLine(response.@return.message);
客户端打印:
queryType is null
服务得到null而不是C#客户端设置的值的原因是什么?我已经在网上搜索过了,没有发现任何与这个问题有关的东西。在Java端,我是否缺少枚举的任何注释?还是VS生成的客户端有问题?谢谢四位的关注。
我提出了一个我并不喜欢的解决方案,但它确实有效。我没有使用enum参数,而是将方法的签名更改为
public OperationResponse serviceOperation(@WebParam(name = "queryType") String queryType)
其中queryType必须是"LOCAL"或"REMOTE"之一,然后我使用enum#valueOf(String)获取枚举实例。我真的需要enum,因为后来我向enum类添加了一个抽象方法,并且每个实例都必须实现特定的行为。