I have spring without spring-boot application. So, I have spring-configuration and web, business and data - layers.
Application changes xml with out World by xml elements. If client try to write some xml in application, his xml maps to jaxb model in @RestController method and then computing occours on businessLayer.
I need to validate xml file before I pass it in business-layer, how can I make it.
There is User jaxb model like this:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement(name = "RiskMetric")
@XmlAccessorType(XmlAccessType.FIELD)
@EqualsAndHashCode
class User{
@XmlElement(name="name")
private String name;
@XmlElement(name="surname")
private String surname;
@XmlElement(name="age")
privage int age;
}
And there is my controller:
@RestController
public class UserController{
@Autowired
private UserBusinessService service;
@RequestMapping(value="/user", method=RequestMethod.POST)
public void add(@RequestBody User user){
service.add(user);
}
}
Can jaxb use just xsd for validate input messages(I didn't find)
I found code fragment like this:
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(locationOfMySchema);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setSchema(schema);
marshaller.marshal(objectToMarshal, new DefaultHandler());
But, how to write interceptor for every entity to use this code fragment?