我们希望用JAXB和JAXB实现一个。我们有一个使用xml的PUT方法,如下所示:
<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。
@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-方法如下所示:
@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方法返回的内容:
<?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发布于 2013-12-20 10:47:20
以下是一些应该有所帮助的项目:
propOrder
下面是将propOrder应用于类的示例。重要的是要记住,您需要包含映射到propOrder中的所有映射的字段/属性。
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)。
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);
}
}发布于 2013-12-20 13:16:12
对于那些必须用实现XML验证的人来说,这是我在上面提示之后使用的PUT方法。现在对我来说很好。
@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();
}https://stackoverflow.com/questions/20699078
复制相似问题