Reputation: 2084
I want to make camel validation against a xsd, but i get an error:
Cannot find the declaration of element 'Address'
I made this problem on smaller xml/xsd files.
validation.xsd:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="Street" type="xs:string" />
<xs:element name="HouseNo" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
xml question:
<?xml version="1.0" encoding="UTF-8"?>
<Address>
<Street>string</Street>
<HouseNo>string</HouseNo>
</Address>
camel config:
@Component
public class CamelRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
restConfiguration().component("servlet") // configure we want to use servlet as the component for the rest DSL
.bindingMode(RestBindingMode.json_xml) // enable json/xml binding mode
.dataFormatProperty("prettyPrint", "true") // output using pretty print
.contextPath("/c/api/")
.apiContextPath("/api-doc") // enable swagger api
.apiProperty("api.version", "2.0.0")
.apiProperty("api.title", "I")
.apiProperty("api.description", "I")
.apiProperty("api.contact.name", "A")
.apiProperty("cors", "true"); // enable CORS
// error handling to return custom HTTP status codes for the various exceptions
onException(StartProcessException.class)
.handled(true)
// use HTTP status 400 when input data is invalid
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400))
.setBody(simple("Invalid input data"));
rest()
.description("I")
.consumes("application/xml").produces("application/xml")
.post("/start").type(Address.class)
.bindingMode(RestBindingMode.json_xml).description("S")
.route().routeId("I").log("Message send: \n ${body}")
.to("validator:file:src/main/resources/validation.xsd")
.endRest();
}
}
Error is:
org.apache.camel.support.processor.validation.SchemaValidationException: Validation failed for: com.sun.org.apache.xerces.internal.jaxp.validation.SimpleXMLSchema@79c47167 errors: [ org.xml.sax.SAXParseException: cvc-elt.1.a: Cannot find the declaration of element 'Address'., Line : 2, Column : 10
Address DTO class:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Address")
@ToString
public class Address {
@XmlElement(name = "Street", required = true)
protected String street;
@XmlElement(name = "HouseNo", required = true)
protected String houseNo;
// getters, setters
}
Upvotes: 0
Views: 901
Reputation: 7015
The error is correct because your XML is not valid according to your schema. You have declared a complex type Address
but the validation is looking for an Element Address
.
Fix your schema like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="Street" type="xs:string"/>
<xs:element name="HouseNo" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Upvotes: 1