我有使用java解析SOAP对象的程序。但是不可能返回解析的SOAP对象。以及如何在网址上传递它。 我的节目是,
public class MarshalDemo {
public static void main(String[] args) throws Exception {
Customer customer = new Customer();
customer.id = 123;
customer.firstName = "Jane";
customer.lastName = "Doe";
QName root = new QName("return");
JAXBElement<Customer> je = new JAXBElement<Customer>(root, Customer.class, customer);
XMLOutputFactory xof = XMLOutputFactory.newFactory();
XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);
xsw.writeStartDocument();
xsw.writeStartElement("S", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
xsw.writeStartElement("S", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
xsw.writeStartElement("ns0", "findCustomerResponse", "http://service.jaxws.blog/");
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.marshal(je, xsw);
xsw.writeEndDocument();
xsw.close();
}
}
class Customer {
int id;
String firstName;
String lastName;
}
答案 0 :(得分:0)
您将希望利用JAX-WS来创建Web服务。您的服务类将如下所示。 JAX-WS将负责创建Envelope
和Body
元素以及通过网络发送数据。 JAX-WS利用JAXB作为对象到XML层,因此您只需要向Customer
类添加任何必要的JAXB注释,JAX-WS会自动将它添加到消息中。
import javax.jws.*;
@WebService
public class FindCustomer {
@WebMethod
public Customer findCustomer(int id) {
Customer customer = new Customer();
customer.setId(id);
customer.setFirstName("Jane");
customer.setLastName("Doe");
return customer;
}
}