2016-02-10 45 views
0

我有簡單的cxf 3.1.1 soap web服務。Apache CXF如何響應自定義驗證錯誤

@WebService 
public interface MyService { 
    @WebMethod 
    public MyResponse addSomeModel(MyRequest req) throws SoapValidationException; 
} 

和實現:

@Component 
@WebService(endpointInterface = "com...MyService", serviceName="Myservice") 
public class MyServiceImpl implements MyService { 
@Override 
    public MyResponse addSomeModel(MyRequest req) throws SoapValidationException { 
     Errors errors = new BeanPropertyBindingResult(req, "myReq"); 
     addCampaignValidator.validate(req, errors); 
     if(errors.hasErrors()){ 
      throw new SoapValidationException("Validation error.", errors); 
     } 
     //... save it the DB  
     return ...; 
    } 
} 

的SoapValidationException是:

@WebFault 
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER) 
public class SoapValidationException extends Exception implements Serializable{ 

    private static final long serialVersionUID = 1L; 
    private Errors errors; 

    public SoapValidationException(String message, Errors errors, Throwable cause) { 
     super(message, cause); 
     this.errors = errors; 
    } 

    public SoapValidationException(String message, Errors errors) { 
     super(message); 
     this.errors = errors; 
    } 

    public List<FieldError> getErrors() { // Here I have to use some complex type 
     return errors.getFieldErrors(); 
    } 

    public String getTargetObject(){ 
     return errors.getObjectName(); 
    } 

    public int getErrorCount(){ 
     return errors.getErrorCount(); 
    } 
} 

當我用一個簡單的類型如int或字符串爲一個公共的getter它的工作原理確定的對象序列化並以xml形式返回。 但我想使用一些複雜的類型,就像我使用Spring驗證中的FieldError一樣。

我要做什麼,所以我可以使用複雜類型作爲SoapValidationException的字段?

不需要使用FieldError,我也可以用我自己的包裝和映射proeprties。

回答

0

由於CXF使用JAXB,所以自定義類型必須是有效的jaxb元素。

我結束了:

@WebFault 
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER) 
public class SoapValidationException extends Exception implements Serializable{ 

    private static final long serialVersionUID = 1L; 
    private Errors errors; 

    public SoapValidationException(String message, Errors errors, Throwable cause) { 
     super(message, cause); 
     this.errors = errors; 
    } 

    public SoapValidationException(String message, Errors errors) { 
     super(message); 
     this.errors = errors; 
    } 

    public String getErrors() { 
     return errors.toString(); 
    } 

    public List<ValidationExceptionDetails> getDetails(){ 
     List<ValidationExceptionDetails> details = new ArrayList<ValidationExceptionDetails>(); 
     for(FieldError fe : errors.getFieldErrors()) { 
      details.add(new ValidationExceptionDetails(fe.getCode(), fe.getDefaultMessage(), fe.getRejectedValue().toString())); 
     } 
     return details; 
    } 
} 

凡ValidationExceptionDetails是:

@XmlRootElement 
public class ValidationExceptionDetails { 
    private String code; 
    private String message; 
    private String rejectedValue; 

    public ValidationExceptionDetails(String code, String message, String rejectedValue) { 
     this.code = code; 
     this.message = message; 
     this.rejectedValue = rejectedValue; 
    } 

    public ValidationExceptionDetails() { 
    } 

    public String getCode() { 
     return code; 
    } 
    @XmlElement 
    public void setCode(String code) { 
     this.code = code; 
    } 
    public String getMessage() { 
     return message; 
    } 
    @XmlElement 
    public void setMessage(String message) { 
     this.message = message; 
    } 
    public String getRejectedValue() { 
     return rejectedValue; 
    } 
    @XmlElement 
    public void setRejectedValue(String rejectedValue) { 
     this.rejectedValue = rejectedValue; 
    } 


}