私は、チェックされた例外を使用して、(たとえば) 間違った入力や間違ったアクションをユーザーに通知したいプロジェクトに取り組んでいます。このような例外には、次のような階層が必要です。
public abstract class BusinessException extends java.lang.Exception {...}
public class InvalidInputException extends BusinessException {...}
public class InvalidActionException extends BusinessException {...}
Maven, jaxws-maven-plugin
, goalを使用して、WSDL/XSD (contract-first アプローチ) から Java コードを生成しwsimport
ます。
私はこの(http://www.ibm.com/developerworks/xml/library/ws-tip-jaxrpc.html)チュートリアルに沿って進めようとしました(jax-rpc用ですが、jax-wsでも動作するようです)同じように)。私が書いた
<definitions ...>
<message name="empty"/>
<message name="ExceptionMessage">
<part name="fault" element="ows:ValidationException"/>
</message>
<portType name="TestWebService">
<operation name="throwException">
<input message="tns:empty"/>
<output message="tns:empty"/>
<fault name="fault" message="tns:ExceptionMessage"/>
</operation>
</portType>
<binding name="TestWebServicePortBinding"
type="tns:TestWebService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="document"/>
<operation name="throwException">
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
<fault name="fault">
<soap:fault name="fault" use="literal"/>
</fault>
</operation>
</binding>
...
</definitions>
ows: 名前空間で定義された型を使用
<xs:complexType name="BusinessException" abstract="true">
<xs:sequence>
<xs:element name="code" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="InvalidInputException">
<xs:complexContent>
<xs:extension base="tns:BusinessException">
<xs:sequence>
<xs:element name="validationMessage" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="InvalidActionException">
<xs:complexContent>
<xs:extension base="tns:BusinessException">
<xs:sequence>
<xs:element name="actionName" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="ValidationException" type="tns:InvalidInputException"/>
を実行するmvn clean package
と、次のようになります (getter、setter、ctor、および注釈が削除されます)。
public interface TestWebService {
@WebMethod
public void throwException() throws ExceptionMessage;
}
public class ExceptionMessage extends Exception {
private InvalidInputException faultInfo;
(...)
}
public abstract class BusinessException implements Serializable {
protected int code;
(...)
}
public class InvalidActionException extends BusinessException implements Serializable {
protected String actionName;
(...)
}
public class InvalidInputException extends BusinessException implements Serializable {
protected String validationMessage;
(...)
}
1 つの例外があり、異なるfaultInto
データを保持できるため、これは私が望んでいたものではありません。上記の例外階層を純粋に XSD/WSDL で作成する方法はありますか? ExceptionMessage
クラスはタグから直接生成されるため<message>
、そこで親子を作成する方法を見つけることができませんでした。