从Java Restful web服务中删除默认XML标记名
我在JAVA上开发RESTWebService时遇到了一个问题 问题:我的Web服务从SQL server数据库获取数据。它包含许多字段。有一个字段包含一个XML标记。因此,我必须创建包含所有数据库字段的XML文件,并将XML标记附加到该文件中 在这里,我可以从数据库中获取数据并构建XML文件,但无法删除默认的XML标记名 如何删除默认的XML标记名?我尝试过@XMLElement选项,但从未成功。请帮忙 电流输出:从Java Restful web服务中删除默认XML标记名,xml,web-services,rest,Xml,Web Services,Rest,我在JAVA上开发RESTWebService时遇到了一个问题 问题:我的Web服务从SQL server数据库获取数据。它包含许多字段。有一个字段包含一个XML标记。因此,我必须创建包含所有数据库字段的XML文件,并将XML标记附加到该文件中 在这里,我可以从数据库中获取数据并构建XML文件,但无法删除默认的XML标记名 如何删除默认的XML标记名?我尝试过@XMLElement选项,但从未成功。请帮忙 电流输出: <CardProfile> <cardID="1"&
<CardProfile>
<cardID="1">
<accountType>1</accountType>
.
.
.
</card>
<ProfileXML><Profile ID="1"><Signature>adfalfjalj/Signature></Profile></ProfileXML>
</CardProfile>
CardProfile.java
package pinpad.repositoryservice;
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "CardProfile")
@XmlAccessorType(XmlAccessType.FIELD)
public class CardProfile {
@XmlElement (name = "Card")
public Card cards = new Card();
public String ProfileXML;
/**
* @return the cards
*/
public Card getCards() {
return cards;
}
/**
* @param cards the cards to set
*/
public void setCards(Card cards) {
this.cards = cards;
}
/**
* @return the profileXML
*/
public String getProfileXML() {
return ProfileXML;
}
/**
* @param profileXML the profileXML to set
*/
public void setProfileXML(String profileXML) {
ProfileXML = profileXML;
}
}
CardProfileRepositoryService.java
package pinpad.repositoryservice;
import javax.ws.rs.*;
/*
* Holds Card and Profiles and exposes REST interface to answer a profile and card data
* based on profile ID and card ID
*/
@Path("/cardprofile")
public class CardProfileRepositoryService {
CardProfile cardprofiles = new CardProfile();
@GET
@Path("/query/")
@Produces("application/xml")
public CardProfile getCardProfile(
@QueryParam("cardid") int CardID,
@QueryParam("profileid") int ProfileID) {
DatabaseManager databaseManager = new DatabaseManager();
databaseManager.getConfigProperties();
databaseManager.getDataBaseConnection();
cardprofiles = databaseManager.executeSprocGetCardProfile(CardID, ProfileID);
databaseManager.closeDataBaseConnection();
return cardprofiles;
}
}
标记值完全来自数据库。所以我想删除标记名,这样它将是唯一的值 这里有一个可行的想法:不必在CardProfile中将XML作为字符串保存,您可以使用一个实际的Java对象Profile,它也有JAXB注释。然后,当您从数据库中提取概要文件字符串时,您将使用JAXB将其解组到概要文件对象,将该对象设置为CardProfile.Profile,然后对整个CardProfile进行marshall
CardProfile {
@XmlElement
public Card cards;
@XmlElement
public Profile Profile;
}
当然,它会增加一点开销,所以如果您有高性能要求,可能不太理想,但它可以工作。一般来说,除非您的XML文档很大,否则封送/解封应该是相当轻量级的操作。请将JAXB注释类的代码添加到您的问题中。对不起,请参阅上述问题中添加的代码。
package pinpad.repositoryservice;
import javax.ws.rs.*;
/*
* Holds Card and Profiles and exposes REST interface to answer a profile and card data
* based on profile ID and card ID
*/
@Path("/cardprofile")
public class CardProfileRepositoryService {
CardProfile cardprofiles = new CardProfile();
@GET
@Path("/query/")
@Produces("application/xml")
public CardProfile getCardProfile(
@QueryParam("cardid") int CardID,
@QueryParam("profileid") int ProfileID) {
DatabaseManager databaseManager = new DatabaseManager();
databaseManager.getConfigProperties();
databaseManager.getDataBaseConnection();
cardprofiles = databaseManager.executeSprocGetCardProfile(CardID, ProfileID);
databaseManager.closeDataBaseConnection();
return cardprofiles;
}
}
CardProfile {
@XmlElement
public Card cards;
@XmlElement
public Profile Profile;
}