2015-10-14 28 views
8

Sto tentando di utilizzare wsdl:fault, ma non posso generare la classe java prevista (eccezione). La classe vengo generato (annotazioni e getter/setter rimossi):wsdl: come generare un'eccezione con errorCode e errorMessage inline?

public class ProjectException extends Exception { 
    private com.home.project.generated.Fault fault; 
} 

public class Fault { 
    protected String errorMessage; 
    protected long errorCode; 
} 

La classe mi aspetto di ottenere generato:

public class ProjectException extends Exception { 
    protected String errorMessage; 
    protected long errorCode; 
} 

mio 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> 

Delle idee come proprietà in linea direttamente nella classe?

Grazie in anticipo

+0

Il wsdl deve rimanere lo stesso? – approxiblue

+0

potrebbe essere cambiato –

risposta

2

Per quanto posso dire, non si può ottenere per generare ciò che vi aspettate.

In base alla specifica JAX-WS 2.1 http://download.oracle.com/otndocs/jcp/jaxws-2.1-mrel2-eval-oth-JSpec/ un errore mappato richiede un'eccezione contenente il bean di errore. L'uso di strumenti che implementano le specifiche ti darà sempre un wrapper Exception annotato con @WebFault che contiene il bean Fault di Java.

0

Si prega di trovare un modo per impostare il servizio web con JAX-WS colpa consegna e la gestione delle eccezioni.

In base alle vostre esigenze, io uso @WebFault annotazione:

interfaccia di servizio Web: implementazione del servizio

package foo.bar; 

import javax.jws.WebMethod; 
import javax.jws.WebService; 

@WebService 
public interface MyProjectService { 

    @WebMethod 
    public String createProject(String name) throws ProjectException; 
} 

Web con colpa:

Qui, simulare un'eccezione in base al nome del progetto.

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 di fagioli:

classe ProjectFault è richiesto dal runtime JAXB per elaborare eccezioni.

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; 
    } 
} 

tuo eccezione personalizzata: ProjectException

Questa classe è necessario per essere annotato con @WebFault annotazione.

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); 
    } 
} 

L'ultimo passo è quello di generare il WSDL e adattare questo esempio.

saluti, André