0

在 JAX-RS 服务中,我不想提供根据XmlValidatonReader位于WebContent目录中的现有 xsd 文件验证传入 XML 文档的方法。

在此处输入图像描述

xsd 文件需要在WebContent文件夹中,因为它应该可供客户端访问。为了验证传入的 XML 文件,我需要 xsd 资源:/WebContent/Category.xsd

我怎样才能从src/at/fhj/ase/XmlValidationReader.java

4

1 回答 1

0

所以我带它跑。

@Provider
@Consumes(MediaType.APPLICATION_XML)
public class XmlValidationReader implements MessageBodyReader<Category> {

    protected Providers providers;
    protected ServletContext servletContext;
    private final String xsdFileName = "/Category.xsd";
    private Schema schema;

    public XmlValidationReader(@Context Providers providers,
            @Context ServletContext servletContext) {
        this.providers = providers;
        this.servletContext = servletContext;

        try {
            SchemaFactory sf = SchemaFactory
                    .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            File xsd = new File(servletContext.getRealPath(xsdFileName));
            schema = sf.newSchema(xsd);
        } catch (Exception e) {
            throw new RuntimeException(
                    "Unable to create XSD validation schema", e);
        }
    }

    @Override
    public boolean isReadable(Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType) {
        ...
    }


    @Override
    public Category readFrom(Class<Category> type, Type genericType,
            Annotation[] annotations, MediaType mediaType,
            MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
            throws IOException, WebApplicationException {
        ...
    }
于 2013-07-24T14:16:40.503 回答