开发者

Not able to append XML element in response

开发者 https://www.devze.com 2023-02-26 16:14 出处:网络
I have created a webservices which accepts XML data and after some computation over server it adds few fields in xml and returns the output to the client. I am using JAX-RS for Restful webservice and

I have created a webservices which accepts XML data and after some computation over server it adds few fields in xml and returns the output to the client. I am using JAX-RS for Restful webservice and JAXB.

Now the problem is when the response is sent back to the client it doesn't include the newly upadated elemnts.

here is the code detail,

class that represent XML (Using 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 getTi开发者_StackOverflowckerId(){
            return m_tickerId;
    }
    public String getGenericTickList()    {
        return m_genericTickList;
    }
    public boolean getSnapShot(){
        return m_snapshot;
    }
    public Contract getContract(){
        return m_Contract;
    }


}

Restful Webservices request function

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

Inside MainAccess class i am doing following

 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 

    }
}

i have created following Market information object and tried to set values of List in between the code

private IBMarketInfo m_marketInfo  = new IBMarketInfo();
 m_marketInfo.setTicker(msg);

XML Request

The problem is i am getting the same XML without appending that mktdata

<?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>


Your problem is in the following code. MainAccess creates an IBMaretInfo, but you are returning the IBMarkettInfo that was passed in unmodified.

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

You model appears to be correctly mapping, since when I run the following code:

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

}

I get the following output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<market>
    <tickerId>0</tickerId>
    <snapshot>false</snapshot>
    <mktdata>FOO</mktdata>
    <mktdata>BAR</mktdata>
</market>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号