2

我需要你的帮助:拜托,拜托。我有一个 Xades 签名的 XML 文档,我收到了一个 byteArray :所以我将我的 byteArray 转换为一个字符串。之后,我尝试解组,以便获得映射的 Java 对象。

结果是我得到了一个“MyJavaObj”实例,其中包含除 Xades-Signature 之外的所有信息。我的签名在 Java 对象中为空,而所有其他信息都映射良好。以下是我的java方法。请帮我获取 MyJavaObj 实例中的对象签名。

<school>
    <documentVersion>2.10</documentVersion>
    <teacher>
    .....
    </teacher>
    <student>
        <name></name>
        <age></age>
        ....
    </student>
    <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="xmldsig-2b72d2f4-4794-4a8b-8cbf-4c74d33629a7">
        <ds:SignedInfo>
        ........
        </ds:SignedInfo>
        .......
    </ds:Signature>
</school>

这是转换的方法

public static MyJavaObj unmarshallBinary(final byte[] pByteStr) {
    try {
        final String xmlFlow = new String(pByteStr, "UTF-8");

        final StringBuffer xmlStr = new StringBuffer(xmlFlow);
        // Unmarshalling with JAXB
        final JAXBContext jaxbContext = JAXBContext.newInstance("generated.package");
        // marshaller
        final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        // Unmarshalling 
        final JAXBElement<MyJavaObj> root = unmarshaller.unmarshal(
            new StreamSource(new StringReader(xmlStr.toString())), MyJavaObj.class);
        return root.getValue();
    } catch (final Throwable excep) {
        excep.printStacktrace();
    }
}

MyJavaObj 结果 = unmarshallBinary(..a ByteArray..);

result.getDocumentVersion() :返回 2.10;

result.getStudent() :返回学生;

result.getSignature() :返回 NULL;

MyJavaObj.java 文件有很好的注释

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "My_Java_Obj", namespace = "urn:my_java_obj", 
propOrder = {"documentVersion", "teacher", "student","signature"})
public class MyJavaObj {

    @XmlElement(name = "documentVersion", required = true)
    protected String documentVersion;
    @XmlElement(name = "teacher", required = true)
    protected Teacher teacher;
    @XmlElement(name = "student", required = true)
    protected Student student;

    @XmlElement(name = "Signature")
    protected Signature signature;
    @XmlAttribute(name = "Id", required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlID
    @XmlSchemaType(name = "ID")
    protected String id;
    .....
    getters and setters
}

谢谢你的帮助。

4

2 回答 2

1

Signature元素位于http://www.w3.org/2000/09/xmldsig#"命名空间中,因此您需要将其包含在@XmlElementforsignature字段中。

@XmlElement(name="Signature", namespace="http://www.w3.org/2000/09/xmldsig#")
protected Signature signature;

了解更多信息

于 2013-04-30T14:42:16.063 回答
0

我的解决方案有点不同

我用了uddi-ws。Maven依赖在这里:

    <!-- https://mvnrepository.com/artifact/org.apache.juddi/uddi-ws -->
    <dependency>
        <groupId>org.apache.juddi</groupId>
        <artifactId>uddi-ws</artifactId>
        <version>3.3.2</version>
    </dependency>

在班上:

//in my case, import org.w3.x2000.x09.xmldsig.SignatureType isn't working
import org.w3._2000._09.xmldsig_.SignatureType;

//Signature declaration    
private SignatureType Signature;

//namespace is important part, without namespace, it returns null
@XmlElement(name = "Signature", namespace = "http://www.w3.org/2000/09/xmldsig#")
public SignatureType getSignature() {
    return Signature;
}

更多信息: https ://www.codesynthesis.com/pipermail/xsd-users/2006-December/000674.html

于 2019-04-17T13:52:46.933 回答