CẬP NHẬT
như theo dõi với điều này: Tôi có thể làm theo cách này ?? nếu xml được trả lại là 4 ..... Nếu tôi đang xây dựng đối tượng Person, tôi tin rằng điều này sẽ bị nghẹt thở. Tôi có thể chỉ liên kết các phần tử xml mà tôi muốn không? nếu Có, tôi có thể làm như thế nào .
Bạn có thể lập bản đồ XML này như sau:
đầu vào.xml
<?xml version="1.0" encoding="UTF-8"?>
<Persons>
<NumberOfPersons>2</NumberOfPersons>
<Person>
<Name>Jane</Name>
<Age>40</Age>
</Person>
<Person>
<Name>John</Name>
<Age>50</Age>
</Person>
</Persons>
Người
package forum7177628;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Persons")
@XmlAccessorType(XmlAccessType.FIELD)
public class Persons {
@XmlElement(name="Person")
private List<Person> people;
}
Person
package forum7177628;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
@XmlElement(name="Name")
private String name;
@XmlElement(name="Age")
private int age;
}
Demo
package forum7177628;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Persons.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Persons persons = (Persons) unmarshaller.unmarshal(new File("src/forum7177628/input.xml"));
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(persons, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Persons>
<Person>
<Name>Jane</Name>
<Age>40</Age>
</Person>
<Person>
<Name>John</Name>
<Age>50</Age>
</Person>
</Persons>
ĐÁP ORIGINAL
Dưới đây là một ví dụ về cách gọi một dịch vụ RESTful sử dụng các API Java SE bao gồm JAXB:
String uri =
"http://localhost:8080/CustomerService/rest/customers/1";
URL url = new URL(uri);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
JAXBContext jc = JAXBContext.newInstance(Customer.class);
InputStream xml = connection.getInputStream();
Customer customer =
(Customer) jc.createUnmarshaller().unmarshal(xml);
connection.disconnect();
Để biết thêm thông tin:
Nguồn
2011-08-24 15:30:14
Nếu bạn có thể gọi dịch vụ và nhận lại json, gson/Jackson Apis dễ dàng hơn jaxb, bạn không cần chú thích trên đối tượng mô hình – Kevin
Xin chào Kevin, tôi có dịch vụ REST bên ngoài và tôi muốn gọi nó từ ứng dụng web. Cách tốt nhất để làm điều đó là gì? Dịch vụ REST trả về định dạng JSON dưới dạng phản hồi. bạn nói thật dễ dàng để xử lý phản hồi JSON. Bạn có thể giải thích như thế nào? – Jignesh