我有使用 java 解析 SOAP 对象的程序。但不可能返回已解析的 SOAP 对象。以及如何在 url 上传递它。 我的程序是,
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;
}
最佳答案
您将要利用 JAX-WS 来创建 Web 服务。您的服务类将类似于以下内容。 JAX-WS 将负责创建 Envelope
和 Body
元素以及通过网络发送数据。 JAX-WS 利用 JAXB 作为对象到 XML 层,因此您只需将任何必要的 JAXB 注释添加到您的 Customer
类,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;
}
}
关于java - 将转换后的 SOAP/XML 对象返回给 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28193758/