2

私は JAXB と RestEasy を使用しています。

Comprobante.class (JAXB 生成クラス) を xml ファイルから返す必要があります。

<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/3" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd">...</cfdi:Comprobante>

私はパッケージ宣言でこれを持っています:

@XmlSchema(
    location = "http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd",
    namespace = "http://www.sat.gob.mx/cfd/3",
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
    attributeFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED,

    xmlns={
        @XmlNs(
                prefix="cfdi",
                namespaceURI="http://www.sat.gob.mx/cfd/3"
                ),
        @XmlNs(
                prefix="xsi",
                namespaceURI="http://www.w3.org/2001/XMLSchema-instance"
                )
        }) 

しかし、XML ファイルから JAXB クラスへのアンマーシャリングの結果には、次のものがありません。

xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd"

印刷するだけです

<cfdi:Comprobante 
xmlns:cfdi="http://www.sat.gob.mx/cfd/3" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">..</cfdi:Comprobante>

私のコードは次のとおりです。

File p = new File(servletContext.getRealPath("/")+factura.getXml().getCfdi());

JAXBContext context = JAXBContext.newInstance("foo.bar.Model.CFDIv32");
Unmarshaller u = context.createUnmarshaller();

return (foo.bar.Comprobante) u.unmarshal(p);

xsi:schemaLocation="" プロパティを配置するように JAXB Unmarshaller に指示するにはどうすればよいですか。

ありがとうございました。

編集:どのように解決したか

私自身の答えを見てください

4

1 に答える 1

2

Rest easy には JAXB Decorators があるため、マーシャリングの前にマーシャラー プロパティに追加できます。

1.- DecoratorProcessor を作成する

@DecorateTypes({"application/xml"})
public class NameSpaceProcesor implements DecoratorProcessor<Marshaller, CustomMarshaller> {

/* Override method</br>
 * @see org.jboss.resteasy.spi.interception.DecoratorProcessor#decorate(java.lang.Object, java.lang.annotation.Annotation, java.lang.Class, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)
 */
@Override
public Marshaller decorate(Marshaller target, CustomMarshaller arg1,
        @SuppressWarnings("rawtypes") Class arg2, Annotation[] arg3, MediaType arg4) {
    try {
        target.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        target.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,"http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd");
    } catch (PropertyException e) {
    }
    return target;
}
}

2.-装飾された注釈を作成します。

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Decorator(processor = NameSpaceProcesor.class, target = Marshaller.class)
public @interface CustomMarshaller {}

3.- 次に、メソッドにデコレータで注釈を付けます。

    @GET
@CustomMarshaller
@Path("cfdi")
@Produces({"application/xml", "application/json"})
// /consulta/cfdi?uuid=0a7da89b-a328-4e54-9666-e1a3d7a10b0a
public Comprobante cfdi(...){}

これが他の人に役立つことを願っています。

于 2013-11-05T23:35:39.580 に答える