【问题标题】:Access xsd file in WebContent folder from a MessageBodyReader class从 MessageBodyReader 类访问 WebContent 文件夹中的 xsd 文件
【发布时间】:2013-07-20 16:38:15
【问题描述】:

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

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

如何从src/at/fhj/ase/XmlValidationReader.java 加载此内容

【问题讨论】:

    标签: java xsd jax-rs getresource web-content


    【解决方案1】:

    所以我带它跑了。

    @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 {
            ...
        }
    

    【讨论】:

      猜你喜欢
      • 2018-02-15
      • 1970-01-01
      • 2015-07-01
      • 2014-02-20
      • 2023-03-13
      • 1970-01-01
      • 2012-12-11
      • 2012-04-09
      • 1970-01-01
      相关资源
      最近更新 更多