I have simple cxf 3.1.1 soap web service.
@WebService
public interface MyService {
@WebMethod
public MyResponse addSomeModel(MyRequest req) throws SoapValidationException;
}
And the Implementation:
@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 ...;
}
}
The SoapValidationException is:
@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();
}
}
When I use a simple type like int or String for a public getter it works ok the object is serialized and returned as xml. But I want to use some complex type, like in the example I use the FieldError
from Spring validation.
What I have to do, so I can use a complex type as a field of SoapValidationException
?
It's not required to use FieldError
, I can also wite my own wrapper and map the proeprties.
Since CXF uses JAXB, the custom type has to be a valid jaxb element.
I ended up with:
@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;
}
}
Where the ValidationExceptionDetails is:
@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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.