1

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?

Roberto
  • 1,288
  • 5
  • 23
  • 47
  • Take a look at [Spring MVC with JAXB, List response based on a Generic class](https://stackoverflow.com/questions/25939629/spring-mvc-with-jaxb-list-response-based-on-a-generic-class), [Spring MVC - set JAXB marshaller property when using @ResponseBody](https://stackoverflow.com/questions/21798952/spring-mvc-set-jaxb-marshaller-property-when-using-responsebody) – Michał Ziober Oct 07 '19 at 10:10

0 Answers0