首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JAXB: JAXB的问题

JAXB: JAXB的问题
EN

Stack Overflow用户
提问于 2013-12-20 08:17:34
回答 2查看 1.3K关注 0票数 2

我们希望用JAXB和JAXB实现一个。我们有一个使用xml的PUT方法,如下所示:

代码语言:javascript
复制
<mailAccount>
    <id>-1</id>
    <name>test</name>
    <mailaddress>test@gmx.de</mailaddress>
    <password>1234</password>
    <servertype>IMAP</servertype>
    <host>hallo</host>
    <port>5678</port>
    <encryption>SSL/TLS</encryption>
    <authentication>true</authentication>
    <interval>12</interval>
</mailAccount>

我们还有一个映射到xml的MailAccount.class。

代码语言:javascript
复制
@XmlRootElement
public class MailAccount {

private String name;
private String mailaddress;
private String password;
private String servertype;
private String host;
private int port;
private String encryption;
private boolean authentication;
private int interval;
private int id;

getter + setter...
}

PUT-方法如下所示:

代码语言:javascript
复制
@PUT()
@Path("/addMailAccount")
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.TEXT_HTML)
public Response addMailAccount(JAXBElement<MailAccount> mail) throws Exception{
    MailAccount mailAccounts = mail.getValue();

    StringWriter sw = new StringWriter();

    JAXBContext jaxbContext = JAXBContext.newInstance(MailAccount.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(mailAccounts, sw);

    String xmlConsume = sw.toString();

    Source source = new StreamSource(new StringReader(xmlConsume));

    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(MailAccountService.class.getResource("/emailAddresses.xsd"));

    Validator validator = schema.newValidator();

    //validator.validate(source);

    return Response.status(200).entity(xmlConsume +"..."+ mailAccounts.getMailadress()).build();
}

我们的目标是编组JAXB-元素,以根据XML验证它。但问题是编组:首先,元素没有按正确的顺序排列。每次使用propOrder标记都会导致内部服务器错误。

第二个问题是"mailaddress“元素是空的。它不是编组,当我将它放入这个方法的响应中时,它的值是空的。

以下是PUT方法返回的内容:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mailAccount>
    <authentication>true</authentication>
    <encryption>SSL/TLS</encryption>
    <host>hallo</host>
    <id>-1</id>
    <interval>12</interval>
    <name>test</name>
    <password>1234</password>
    <port>5678</port>
    <servertype>IMAP</servertype>
</mailAccount>
...null
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-20 10:47:20

以下是一些应该有所帮助的项目:

propOrder

下面是将propOrder应用于类的示例。重要的是要记住,您需要包含映射到propOrder中的所有映射的字段/属性。

代码语言:javascript
复制
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlType(propOrder={"id", "name", "mailaddress", "password", "servertype", "host", "port", "encryption", "authentication", "interval"})
@XmlAccessorType(XmlAccessType.FIELD)
public class MailAccount {

    private String name;
    private String mailaddress;
    private String password;
    private String servertype;
    private String host;
    private int port;
    private String encryption;
    private boolean authentication;
    private int interval;
    private int id;

}

作为操作的一部分执行架构验证的

下面是一个独立的示例,演示如何利用模式验证作为封送处理操作的一部分,而不是将其作为单独的操作(请参阅:http://blog.bdoughan.com/2010/12/jaxb-and-marshalunmarshal-schema.html)。

代码语言:javascript
复制
import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.bind.*;
import javax.xml.validation.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(MailAccount.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum20699078/input.xml");
        MailAccount mailAccount = (MailAccount) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(MailAccountService.class.getResource("/emailAddresses.xsd"));
        marshaller.setSchema(schema);

        marshaller.marshal(mailAccount, System.out);
    }

}
票数 3
EN

Stack Overflow用户

发布于 2013-12-20 13:16:12

对于那些必须用实现XML验证的人来说,这是我在上面提示之后使用的PUT方法。现在对我来说很好。

代码语言:javascript
复制
@PUT()
@Path("/addMailAccount")
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.TEXT_HTML)
public Response addMailAccount(JAXBElement<MailAccount> mail) throws MailAccountServiceException{
    String xmlConsume = "";

    try{
        MailAccount mailAccounts = mail.getValue();

        StringWriter sw = new StringWriter();

        JAXBContext jaxbContext = JAXBContext.newInstance(MailAccount.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(MailAccountService.class.getResource("/mailAccount.xsd"));
        jaxbMarshaller.setSchema(schema);

        jaxbMarshaller.marshal(mailAccounts, sw);

        xmlConsume = sw.toString();
    }
    catch(Exception e){
        throw new MailAccountServiceException("Fehlerhafte Anfrage: " + e.getCause().getMessage());
    }

    return Response.status(200).entity(xmlConsume).build();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20699078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档