Java 将转换后的SOAP/XML对象返回到url
我有一个使用java解析SOAP对象的程序。但是不可能返回已解析的SOAP对象。以及如何在url上传递。 我的节目是,Java 将转换后的SOAP/XML对象返回到url,java,xml,soap,jaxb,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";
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;
}
公共类封送处理演示{
公共静态void main(字符串[]args)引发异常{
客户=新客户();
customer.id=123;
customer.firstName=“Jane”;
customer.lastName=“Doe”;
QName root=新的QName(“返回”);
JAXBElement je=新的JAXBElement(root,Customer.class,Customer);
XMLOutputFactory xof=XMLOutputFactory.newFactory();
XMLStreamWriter xsw=xof.createXMLStreamWriter(System.out);
xsw.writeStartDocument();
writeStarteElement(“S”、“信封”、“信封”http://schemas.xmlsoap.org/soap/envelope/");
writeStarteElement(“S”,“Body”,”http://schemas.xmlsoap.org/soap/envelope/");
writeStarteElement(“ns0”、“findCustomerResponse”、”http://service.jaxws.blog/");
JAXBContext jc=JAXBContext.newInstance(Customer.class);
Marshaller=jc.createMarshaller();
setProperty(marshaller.JAXB_片段,true);
元帅,元帅(je,xsw);
xsw.writeEndDocument();
xsw.close();
}
}
类客户{
int-id;
字符串名;
字符串lastName;
}
您需要利用JAX-WS来创建Web服务。您的服务类将如下所示。JAX-WS将负责创建信封
和主体
元素,并通过线路发送数据。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;
}
}
对不起,我听不懂你说什么。如何将转换后的SOAP对象返回到URL您计划如何运行Web服务?我假设您将使用一个应用服务器,如GlassFish、WebLogic、WebSphere等。我使用的是spring工具及其支付流程。我想使用
POST
方法将转换后的SOAP
转发到url中。它返回一些SOAP
对象。然后我解析为字符串并在我的程序中使用