在我的项目中,我们必须首先调用具有相同请求/响应对象的2个协定SOAP Web服务。
由于Web服务是先契约的,我用CXF wsdl 2 java创建了类,现在我有两个包中的两组相同的类。有没有办法我可以将这些类重构成一组类?
为了描述更多,我有2个包,每个包包含以下类,具有完全相同的属性和代码:
@Getter
@Setter
@ToString
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"loginRequest", "request"})
@XmlRootElement(name = "requestHolder")
public final class RequestHolder {
@XmlElement(required = true)
private LoginRequest loginRequest;
@XmlElement(required = true)
private RequestData request;
}
和
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "LoginRequest", propOrder = {"username", "password"})
public final class LoginRequest {
@NotBlank
@XmlElement(required = true)
private String username;
@NotBlank
@ToString.Exclude
@XmlElement(required = true)
private String password;
}
和
@Getter
@Setter
@ToString
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "requestData")
public final class RequestData {
@XmlElement(required = true)
private String statementId;
@XmlElement(required = true)
private String tracker;
@XmlElement(required = true)
private int depositCount;
@XmlElement(required = true)
private long depositAmount;
@XmlElement(required = true)
private int withdrawalCount;
@XmlElement(required = true)
private long withdrawalAmount;
@XmlElement(required = true)
private int unknownCount;
@XmlElement(required = true)
private long unknownAmount;
@XmlElement(required = true)
private int duplicateCount;
@XmlElement(required = true)
private long duplicateAmount;
@XmlElement(required = true)
private int okCount;
@XmlElement(required = true)
private long okAmount;
@XmlElement(required = true)
private String processCode;
@XmlElement(required = true)
private String processMessage;
}
和
@Getter
@Setter
@ToString
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"exception", "result"})
@XmlRootElement(name = "responseHolder")
public final class ResponseHolder {
@XmlElement(name = "Exception", required = true)
private ExceptionHolder exception;
@XmlElement(name = "Result")
private Result result;
}
和
@Getter
@Setter
@ToString
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "exceptionHolder", propOrder = {"code", "message"})
public final class ExceptionHolder {
@XmlElement(name = "Code", required = true)
private String code;
@XmlElement(name = "Message", required = true)
private String message;
}
和
@Getter
@Setter
@ToString
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "result")
public final class Result {
@XmlElement(name = "Receipt", required = true, nillable = true)
private String receipt;
}
然后,我有2个WebService接口,它们也彼此相似(绑定到不同的端点),具有不同的WebService方法名称
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@WebService(targetNamespace = Service1.TARGET_NAMESPACE, name = "Service1")
@XmlSeeAlso(ObjectFactory.class)
public interface Service1 {
String TARGET_NAMESPACE = "http://www.mycompany.com/service1Namespace";
@WebMethod
@WebResult(name = "responseHolder", targetNamespace = TARGET_NAMESPACE, partName = "responseHolder")
ResponseHolder serviceMethod1(
@WebParam(partName = "requestHolder", name = "requestHolder", targetNamespace = TARGET_NAMESPACE) RequestHolder requestHolder);
}
和
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@WebService(targetNamespace = Service2.TARGET_NAMESPACE, name = "Service2")
@XmlSeeAlso(ObjectFactory.class)
public interface Service2 {
String TARGET_NAMESPACE = "http://www.anotherCompany.com/service2Namespace";
@WebMethod
@WebResult(name = "responseHolder", targetNamespace = TARGET_NAMESPACE, partName = "responseHolder")
ResponseHolder serviceMethod2(
@WebParam(partName = "requestHolder", name = "requestHolder", targetNamespace = TARGET_NAMESPACE) RequestHolder requestHolder);
}
正如你所看到的,只有命名空间和服务端点以及方法名称不同。除了这两个服务接口,还有两个package-info.java
文件,它们用不同的命名空间进行了注解。
当我试图将公共类移动到另一个包中并使用它们时,由于它们与带注解的package-info.java
文件不在同一个包中,因此它们的命名空间没有正确添加到请求信封中,并且我在服务端点处遇到错误。
所以问题是,我如何使用一组请求/响应类,这可能吗?
1条答案
按热度按时间wko9yo5t1#
这有点复杂,但是您可以指定(当应用wsdl2java时)一个“绑定文件”,它允许您通过XJC自定义Java类的生成方式(例如包名、类型Map等......)
在您的特定情况下,您应该查看“episode":
.episode文件由XJC(XML Schema to Java)编译器生成。它是一个将模式类型与现有类相关联的模式绑定。当您有一个由其他模式导入的XML模式时,它很有用,因为它会阻止重新生成模型。
(Old但是)有趣的文章:
https://dzone.com/articles/reusing-generated-jaxb-classes