我创建了一个接受XML数据的web服务,在服务器上进行一些计算之后,它在xml中添加了几个字段并将输出返回给客户端。我正在使用JAX-RS for Restful webservice和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;
}
}
Restful Webservices请求功能
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请求
问题是我得到相同的XML而没有附加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>
答案 0 :(得分:1)
您的问题在以下代码中。 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>