java - 将转换后的 SOAP/XML 对象返回给 url

标签 java xml soap jaxb

我有使用 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 将负责创建 EnvelopeBody 元素以及通过网络发送数据。 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/

相关文章:

java - 循环遍历SoapMessage主体中的所有元素并返回我想要的节点

java - 将自定义项目应用于微调器的下拉 View 时如何删除白色条纹?

java - 单一方法的 JUnit 测试清理

javascript - 如何在 Odoo 中聚焦一个 TransientModel 形式的字段?

jquery - 本地 html 文件 AJAX 调用和 jQuery 问题

android - 如何放置此符号 : "' "(simple Quotation mark) in a XMLfile of android?

python - 泡沫抛出错误 'type not found' 使用 SOAP 服务

java - 在 Java 中自定义 SOAP header

java - Android 应用程序登录屏幕未加载

java - 尝试返回自定义对象类型时出现 SerializationException