我正在尝试使用wsdl:fault
,但无法生成预期的java类(异常)。我生成的类(删除了注释和getter / setter):
public class ProjectException extends Exception {
private com.home.project.generated.Fault fault;
}
public class Fault {
protected String errorMessage;
protected long errorCode;
}
我期望获得的课程:
public class ProjectException extends Exception {
protected String errorMessage;
protected long errorCode;
}
我的wsdl:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="ProjectSoapServiceImplService"
targetNamespace="http://www.home.com/webservices/v1_0/project/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://www.home.com/webservices/v1_0/project/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
<wsdl:types>
<xs:schema xmlns:tns="http://www.home.com/webservices/v1_0/project/"
xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified"
targetNamespace="http://www.home.com/webservices/v1_0/project/" version="1.0">
<xs:element name="createProject" type="tns:projectRequest"/>
<xs:element name="projectResponse" type="tns:projectResponse"/>
<xs:complexType name="projectRequest">
<xs:sequence>
<xs:element minOccurs="0" name="projectName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="projectResponse">
<xs:sequence>
<xs:element minOccurs="0" name="projectId" type="xs:long"/>
</xs:sequence>
</xs:complexType>
<xs:element name="fault">
<xs:complexType>
<xs:sequence>
<xs:element name="errorMessage" type="xs:string"/>
<xs:element name="errorCode" type="xs:long"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="createProject">
<wsdl:part name="parameters" element="tns:createProject"/>
</wsdl:message>
<wsdl:message name="createProjectResponse">
<wsdl:part name="parameters" element="tns:projectResponse">
</wsdl:part>
</wsdl:message>
<wsdl:message name="projectException">
<wsdl:part name="faultMessage" element="tns:fault"/>
</wsdl:message>
<wsdl:portType name="ProjectPort">
<wsdl:operation name="createProject">
<wsdl:input name="createProject" message="tns:createProject"/>
<wsdl:output name="createProjectResponse" message="tns:createProjectResponse"/>
<wsdl:fault name="fault" message="tns:projectException"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ProjectSoapServiceImplServiceSoapBinding" type="tns:ProjectPort">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="createProject">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="createProject">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="createProjectResponse">
<soap:body use="literal"/>
</wsdl:output>
<wsdl:fault name="fault">
<soap:fault name="fault" use="literal"/>
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ProjectSoapServiceImplService">
<wsdl:port name="ProjectPortPort" binding="tns:ProjectSoapServiceImplServiceSoapBinding">
<soap:address location="http://localhost:9090/ProjectPortPort"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
如何直接在课堂中内联属性?
提前致谢
答案 0 :(得分:3)
据我所知,你无法实现产生你期望的效果。
根据规范JAX-WS 2.1 http://download.oracle.com/otndocs/jcp/jaxws-2.1-mrel2-eval-oth-JSpec/,映射的错误需要包含故障bean的异常。使用实现规范的工具将始终为您提供一个使用@WebFault注释的包含Java Fault bean的Exception包装器。
答案 1 :(得分:0)
请找到一种使用JAX-WS故障处理和异常处理来设置Web服务的方法。
根据您的需要,我使用 @WebFault 注释:
网络服务界面:
package foo.bar;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface MyProjectService {
@WebMethod
public String createProject(String name) throws ProjectException;
}
带有错误的Web服务实现:
在这里,我们根据项目名称模拟异常。
package foo.bar;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(serviceName="MyProjectService", portName="MyProjectServiceService")
public class MyProjectServiceImpl implements MyProjectService {
public MyProjectServiceImpl() {
}
@WebMethod
public String createProject(@WebParam(name="name") String name) throws ProjectException {
if (name.equalsIgnoreCase("bad name")) {
ProjectFault fault = new ProjectFault();
fault.setFaultCode("123");
fault.setFaultString("Custom error message");
throw new ProjectException("123","Custom error message");
} else {
return "Project created : " + name;
}
}
}
ProjectFault bean:
JAXB运行时需要ProjectFault类来处理异常。
package foo.bar;
public class ProjectFault {
private String faultCode;
private String faultString;
public String getFaultCode() {
return faultCode;
}
public void setFaultCode(String faultCode) {
this.faultCode = faultCode;
}
public String getFaultString() {
return faultString;
}
public void setFaultString(String faultString) {
this.faultString = faultString;
}
}
您的自定义异常:ProjectException
此类需要使用@WebFault批注进行批注。
package foo.bar;
import javax.xml.ws.WebFault;
@WebFault(name="ProjectFault")
public class ProjectException extends Exception {
private ProjectFault fault;
public ProjectException() {
}
protected ProjectException(ProjectFault fault) {
super(fault.getFaultString());
this.fault = fault;
}
public ProjectException(String message, ProjectFault faultInfo){
super(message);
this.fault = faultInfo;
}
public ProjectException(String message, ProjectFault faultInfo, Throwable cause){
super(message,cause);
this.fault = faultInfo;
}
public ProjectFault getFaultInfo(){
return fault;
}
public ProjectException(String message) {
super(message);
}
public ProjectException(String code, String message) {
super(message);
this.fault = new ProjectFault();
this.fault.setFaultString(message);
this.fault.setFaultCode(code);
}
public ProjectException(Throwable cause) {
super(cause);
}
public ProjectException(String message, Throwable cause) {
super(message, cause);
}
}
最后一步是生成WSDL并调整此示例。
问候,André