从Java Restful Web服务中删除默认XML标记名称

时间:2012-11-07 12:50:23

标签: xml web-services rest

我在JAVA上的REST Web服务开发中面临问题。

问题:我的webservice从SQL Server数据库中获取数据。它包含多个字段。有一个字段包含一个XML标记。因此,我必须创建包含所有Database字段的XML文件,并将XML标记附加到其中。

在这里,我可以从数据库中获取数据并构建XML文件但我无法删除默认的XML标记名称。

如何删除默认的XML标记名称?我尝试过@XMLElement选项,但从未成功过。请帮忙!

当前输出:

<CardProfile>
    <cardID="1">
    <accountType>1</accountType>
    .
    .
    .   
    </card>
    <ProfileXML><Profile ID="1"><Signature>adfalfjalj/Signature></Profile></ProfileXML>
</CardProfile>

期望的输出:

<CardProfile>
    <card cardID="1">
    <accountType>1</accountType>
    .
    .
    .   
    </card>
    <Profile ID="1"><Signature>adfalfjalj/Signature></Profile>
</CardProfile>

Card.java

package pinpad.repositoryservice;

import javax.xml.bind.annotation.*;

public class Card {
    private int CardID;
    private String CardHolderName;
    private String Track1Data; 
    private String Track2Data;
    private String Track3Data;
    private String PANNumber;
    private String ExpiryDate;
    private String CVV;
    private String Address;
    private String ZipCode;
    private String DriverID;
    private String AccountTypeCreditDebit;

    @XmlAttribute (name = "ID") 
    public int getCardID() {
        return CardID;
    }

    public void setCardID(int id) {
        this.CardID = id;
    }

    @XmlElement
    public String getCardHolderName() {
        return CardHolderName;
    }

    public void setCardHolderName(String CardHolderName) {
        this.CardHolderName = CardHolderName;
    }

    @XmlElement
    public String getTrack1Data() {
        return Track1Data;
    }

    public void setTrack1Data(String Track1Data) {
        this.Track1Data = Track1Data;
    }

    @XmlElement
    public String getTrack2Data() {
        return Track2Data;
    }

    public void setTrack2Data(String Track2Data) {
        this.Track2Data = Track2Data;
    }

    @XmlElement
    public String getTrack3Data() {
        return Track3Data;
    }

    public void setTrack3Data(String Track3Data) {
        this.Track3Data = Track3Data;
    }

    @XmlElement
    public String getPANNumber() {
        return PANNumber;
    }

    public void setPANNumber(String PANNumber) {
        this.PANNumber = PANNumber;
    }

    @XmlElement
    public String getExpiryDate() {
        return ExpiryDate;
    }

    public void setExpiryDate(String ExpiryDate) {
        this.ExpiryDate = ExpiryDate;
    }

    @XmlElement
    public String getCVV() {
        return CVV;
    }

    public void setCVV(String CVV) {
        this.CVV = CVV;
    }

    @XmlElement
    public String getAddress() {
        return Address;
    }

    public void setAddress(String Address) {
        this.Address = Address;
    }

    @XmlElement
    public String getZipCode() {
        return ZipCode;
    }

    public void setZipCode(String ZipCode) {
        this.ZipCode = ZipCode;
    }

    @XmlElement
    public String getDriverID() {
        return DriverID;
    }

    public void setDriverID(String DriverID) {
        this.DriverID = DriverID;
    }

    @XmlElement
    public String getAccountTypeCreditDebit() {
        return AccountTypeCreditDebit;
    }

    public void setAccountTypeCreditDebit(String AccountTypeCreditDebit) {
        this.AccountTypeCreditDebit = AccountTypeCreditDebit;
    }
}

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;
    }
}

标记值完全来自数据库。所以我想删除标签名称,因此它只是Value ..。

1 个答案:

答案 0 :(得分:0)

这是一个应该工作的想法:不是在CardProfile中将XML保持为String,而是可以有一个实际的Java对象Profile,它也有JAXB注释。然后,当您从数据库中提取Profile String时,您将使用JAXB将其解组为Profile Object,将该对象设置为CardProfile.profile,然后编组整个CardProfile。

CardProfile {
  @XmlElement
  public Card cards;

  @XmlElement
  public Profile Profile;
 }

当然它会增加一些开销,所以如果你有高性能要求可能不合适,但它会起作用。通常,除非您的XML文档很大,否则编组/解组应该是相当轻量级的操作。