【问题标题】:How to validate XML REST request in Springboot with Jaxb?如何使用 Jaxb 在 Springboot 中验证 XML REST 请求?
【发布时间】:2018-07-06 12:42:00
【问题描述】:

我认为这就像添加注释一样简单,但我找不到解决方案。

我有一个接受 XML 请求正文的简单端点:

@RequestMapping(value = "/import", method = RequestMethod.POST, consumes = MediaType.TEXT_XML_VALUE)
public ResponseEntity<Result> importReceipts(@Valid @RequestBody ImportRequest request) throws Exception {

其中 ImportRequest 是从 XSD 生成的 JAXB 类。当客户端发送请求时,这可以正常工作,但如果请求无效,则不会出现错误。

任何人都可以建议在给定 XSD 的情况下验证此请求正文的最佳方法吗?

谢谢

【问题讨论】:

标签: spring rest validation xsd jaxb


【解决方案1】:

谢谢亚历克斯,

我之前看到了这个响应,但我再次查看了我的代码并发现了错误:)

@Bean
public MarshallingHttpMessageConverter marshallingHttpMessageConverter()
{
    MarshallingHttpMessageConverter marshallingHttpMessageConverter = new MarshallingHttpMessageConverter();

    marshallingHttpMessageConverter.setMarshaller(jaxb2Marshaller());
    marshallingHttpMessageConverter.setUnmarshaller(jaxb2Marshaller());

    return marshallingHttpMessageConverter;
}

@Bean
public Jaxb2Marshaller jaxb2Marshaller()
{
    Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
    jaxb2Marshaller.setSchemas(new ClassPathResource("Import.xsd"), new ClassPathResource("BasicTypes.xsd"));
    jaxb2Marshaller.setClassesToBeBound(Import.class);
    return jaxb2Marshaller;
}

我有一个错字,但主要问题是我不止一次调用 jaxb2Marshaller.setSchemas,第二次调用删除了第一个模式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-26
    • 2012-01-05
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多