具有模式验证的 JAX-RS

发布于 2024-12-04 02:59:34 字数 849 浏览 0 评论 0原文

我有一个 RESTful 服务,它有一个路径参数和一个从请求正文中解组的参数。请求正文参数是 XML,我有一个 XSD。我一直在尝试根据 XSD 验证 XML 有效负载,但无济于事。我已经尝试了以下操作,如此处所述

<jaxrs:server address="/"
    serviceClass="my.endpoint.class">
    <jaxrs:schemaLocations>
        <jaxrs:schemaLocation>classpath:schema/myschema.xsd</jaxrs:schemaLocation>
    </jaxrs:schemaLocations>
</jaxrs:server>

正在找到模式(至少没有错误),但我期望无效的有效负载不会引发异常。与 XSD 内容不匹配的参数将显示为 null。它可能不相关,但我的自动生成的有效负载类具有三个属性,其中一些是必需的。

我已经简单地创建了一个 MessageBodyReader,如 此处< /a> 但我认为我遇到了范围问题,并且在调用 readFrom 时我的架构对象不可用。

任何帮助或建议将不胜感激!

I have a RESTful service that has a single path parameter and a parameter that is unmarshalled from the request body. The request body parameter is XML for which I have an XSD. I've been trying to validate the XML payload against the XSD but to no avail. I've tried the following, as described here:

<jaxrs:server address="/"
    serviceClass="my.endpoint.class">
    <jaxrs:schemaLocations>
        <jaxrs:schemaLocation>classpath:schema/myschema.xsd</jaxrs:schemaLocation>
    </jaxrs:schemaLocations>
</jaxrs:server>

The schemas are being found (there are no errors at least) but what I expect to be an invalid payload is not throwing an exception. Parameters that don't match the XSD contents are coming through as null. It may not be relevant but my auto-generated payload class has three attributes, some of which are required.

I've had a brief go at creating a MessageBodyReader, as described here but I think I'm having scope issues and my schema object is not available when readFrom is called.

Any help or suggestions would be gratefully appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2024-12-11 02:59:34

结果我使用的 servlet 不接受上面显示的 jaxrs 配置。我从使用这个: 更改

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

为:

<servlet-class>
    org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<init-param>
    <param-name>config-location</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>

我的 applicationContext.xml

<jaxrs:server address="/">
    <jaxrs:schemaLocations>
        <jaxrs:schemaLocation>classpath:schema/myschema1.xsd</jaxrs:schemaLocation>
        <jaxrs:schemaLocation>schema/myschema2.xsd</jaxrs:schemaLocation>
        <jaxrs:schemaLocation>schema/myschema3.xsd</jaxrs:schemaLocation>
    </jaxrs:schemaLocations>

    <jaxrs:serviceBeans>
        <bean class="my.package.endPoint1" />
        <bean class="my.package.endPoint2" /> 
    </jaxrs:serviceBeans>

    <jaxrs:features>
        <cxf:logging />
    </jaxrs:features>
</jaxrs:server>

架构引用的片段来自与 WEB-INF 相邻的资源目录。

Turns out the servlet I was using wasn't accepting the jaxrs configuration shown above. I changed from using this:

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

to this:

<servlet-class>
    org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<init-param>
    <param-name>config-location</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>

Snippet of my applicationContext.xml

<jaxrs:server address="/">
    <jaxrs:schemaLocations>
        <jaxrs:schemaLocation>classpath:schema/myschema1.xsd</jaxrs:schemaLocation>
        <jaxrs:schemaLocation>schema/myschema2.xsd</jaxrs:schemaLocation>
        <jaxrs:schemaLocation>schema/myschema3.xsd</jaxrs:schemaLocation>
    </jaxrs:schemaLocations>

    <jaxrs:serviceBeans>
        <bean class="my.package.endPoint1" />
        <bean class="my.package.endPoint2" /> 
    </jaxrs:serviceBeans>

    <jaxrs:features>
        <cxf:logging />
    </jaxrs:features>
</jaxrs:server>

Schema references are from the resources directory, adjacent to WEB-INF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文