无法在响应中附加XML元素

| 我创建了一个webservices,它接受XML数据,并在服务器上进行一些计算后,在xml中添加了几个字段并将输出返回给客户端。我正在将JAX-RS用于Restful Web服务和JAXB。 现在的问题是,将响应发送回客户端时,它不包含新更新的元素。 这是代码详细信息, 表示XML的类(使用JAXB)
@XmlRootElement(name = \"market\")
@XmlAccessorType(XmlAccessType.FIELD)
public class IBMarketInfo {

    @XmlElement(name=\"contract\")
    Contract m_Contract;

    @XmlElement(name=\"tickerId\")
    int m_tickerId;

    @XmlElement(name=\"tickList\")
    String m_genericTickList;

    @XmlElement(name=\"snapshot\")
    boolean m_snapshot;


    @XmlElement(name=\"mktdata\") // I AM NOT BE ABLE TO VIEW THIS ELEMENT IN THE RESPONSE
    List<String>m_Ticker;




    public IBMarketInfo(){

    }
 public void setTicker(String data){
         if (m_Ticker == null) {
                m_Ticker = new ArrayList<String>();
            }
      m_Ticker.add(data);

    }
    public List<String> getTicker(){

        if (m_Ticker == null) {
                m_Ticker = new ArrayList<String>();
            }

        return m_Ticker;
    }
    public void setTickerId(int tickerid){
        m_tickerId = tickerid;
    }
    public void setGenericTickList(String ticklist){
        m_genericTickList = ticklist;
    }
    public void setSnapshot(boolean snapshot){
        m_snapshot=snapshot;
    }
    public void setContract(Contract contract){
        m_Contract = contract;
    }
    public int getTickerId(){
            return m_tickerId;
    }
    public String getGenericTickList()    {
        return m_genericTickList;
    }
    public boolean getSnapShot(){
        return m_snapshot;
    }
    public Contract getContract(){
        return m_Contract;
    }


}
宁静的Web服务请求功能
public JAXBElement<IBMarketInfo>getMarketData(JAXBElement<IBMarketInfo> info){

    MainAccess ma = new MainAccess(); // MainAccess Will pull the data from external server
    IBMarketInfo market = info.getValue(); 

   ma.onRequestData(market.getTickerId(),market.getContract(),market.getGenericTickList(),
   market.getSnapShot()); // set the user given input from xml 

  return info;
}
在MainAccess类中,我正在关注
 public class MainAccess {

   private IBMarketInfo m_marketInfo  = new IBMarketInfo(); //declaring in class

    // as an when data comes these following functions will add data into List
    public void tickSize( int tickerId, int field, int size) {

        String msg = EWrapperMsgGenerator.tickSize( tickerId, field, size);
        m_marketInfo.setTicker(msg); // setting m_Ticker 

    }
    public void tickPrice( int tickerId, int field, double price, int canAutoExecute) {

        String msg = EWrapperMsgGenerator.tickPrice( tickerId, field, price, canAutoExecute);

        m_marketInfo.setTicker(msg); //setting m_Ticker 

    }
}
我创建了以下市场信息对象,并尝试在代码之间设置List的值
private IBMarketInfo m_marketInfo  = new IBMarketInfo();
 m_marketInfo.setTicker(msg);
XML请求 问题是我在不附加该mktdata的情况下获得了相同的XML
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<market>
    <contract>
        <symbol>IBM</symbol>
        <sectype>STK</sectype>
        <exchange>SMART</exchange>
        <currency>USD</currency>    
    </contract> 
        <tickerId>1</tickerId>
    <tickList>1212,12121</tickList>
    <snapshot>false</snapshot>
<ticker-data></ticker-data>
</market>
已邀请:
您的问题在以下代码中。 MainAccess创建一个IBMaretInfo,但是您将返回未经修改传递的IBMarkettInfo。
public JAXBElement<IBMarketInfo>getMarketData(JAXBElement<IBMarketInfo> info){

    MainAccess ma = new MainAccess(); // MainAccess Will pull the data from external server
    IBMarketInfo market = info.getValue(); 

   ma.onRequestData(market.getTickerId(),market.getContract(),market.getGenericTickList(),
   market.getSnapShot()); // set the user given input from xml 

  return info;
}
自从我运行以下代码以来,您的模型似乎已正确映射:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

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

        IBMarketInfo ibmi = new IBMarketInfo();
        ibmi.setTicker(\"FOO\");
        ibmi.setTicker(\"BAR\");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(ibmi, System.out);
    }

}
我得到以下输出:
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
<market>
    <tickerId>0</tickerId>
    <snapshot>false</snapshot>
    <mktdata>FOO</mktdata>
    <mktdata>BAR</mktdata>
</market>

要回复问题请先登录注册