我使用Thymeleaf作为UI框架,使用Java作为后端,我正在创建一个休息Web服务。使用JAXB从URL解组xml。我认为我的控制器弄乱了一些东西,这就是我无法打印完整列表的原因。
XML
<results>
<rootNode>
<node>1336</node>
<state>CL</state>
<time>0</time>
<ip_addresses>
<ip_address type="DOC">06:56:43.0</ip_address>
<ip_address type="PE">06:56:43.0</ip_address>
</ip_addresses>
<lease_date>2017-01-25</lease_date>
</rootNode>
</results>
控制器
经过多次测试后,我缩小了问题的原因是在我的控制器中。
@Controller
public class IndexController {
@Autowired
JAXSample index;
@RequestMapping(value = "/getDetails", method = RequestMethod.POST)
public ModelAndView getDetails(Info info) throws MalformedURLException {
ModelAndView model = new ModelAndView("getDetails");
//JAXSample index = new JAXSample();
readingXml cinfo = index.readXML(info.getInputMacAddress());
model.addObject("cinfo", cinfo);
System.out.println(cinfo.getip_Addresses().toString() + "FROM CONTROLLER");
return model;
}
//****Should be [06:56:43.0, 06:56:43.0] ******
Output: [06:56:43.0, ] FROM FROM CONTROLLER
JaxSample.class
jaxb示例找到@ example
public readingXml readXML(String MAC) throws MalformedURLException {
results customer = null;
readingXml = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(results.class);
URL url = new URL("some type of url for xml page" + MAC);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.addRequestProperty("User-Agent", "Mozilla/4.76");
InputStream is = http.getInputStream();
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
customer = (results) jaxbUnmarshaller.unmarshal(is);
List<readingXml> readingXml = customer.getReadingXml();
readingXml = readingXmls.get(0);
} catch (JAXBException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return readingXml;
}
因此,出于测试目的,我尝试打印出列表值。
System.out.println(readingXml.ip_Addresses().toString() + "FROM SAMPLE");
//****Should be [06:56:43.0, 06:56:43.0] ******
//output [06:56:43.0, ] FROM SAMPLE
BUT !!当我将相同的确切代码复制到我的 main.class 中并执行相同的请求时。
System.out.println(readingXml.ip_Addresses().toString() + "FROM MAINCLASS");
//output [06:56:43.0, 06:56:43.0] FROM MAINCLASS
我得到了我想要的价值观。为什么在JAXB或我的控制器中这不起作用?