简体   繁体   中英

Consume SOAP XML in Rest WebService

I am able to consume xml based request in rest webservice. Below is the sample code how I am consuming XML request in my rest webservice.

Now, I want to consume soap request XML and I know SOAP is a messaging protocol. It can only be consumed by SOAP based webservices but it uses an XML data format to declare its request and response messages, relying on XML Schema. I just want to recevice its XML based request in rest webservice.

Is there any way around to cater this requirement in rest based web service.

Below is the sample soap request which I want to consume in rest webservice.

SOAP Request:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:int="http://example.com/">
    <soapenv:Header/>
    <soapenv:Body>
        <order>
            <order-no></order-no>
            <custmer></custmer>
            <address></address>
            <bill-amount></bill-amount>
        </order>
    </soapenv:Body>
</soapenv:Envelope>

Below sample code of how I am consuming XML request in my rest webservice.

Sample Code:

Rest Endpoint for Consuming XML Request

    @Path("/processOrder")
    @POST
    @Produces("application/xml")
    @Consumes("application/xml")
    public OrderResponse processOrder(Order order) {
    return processUser(order);
    }

Order.java

    @XmlRootElement(name = "order")
    public class Order {    
    private int orderNo;
    private String custmer;
    private String address;
    private String amount;
     
    @XmlAttribute(name = "order-no")
    public int getOrderNo() {
        return orderNo;
    }
    public void setOrderNo(int orderNo) {
        this.orderNo = orderNo;
    }
     
    @XmlElement
    public String getCustmer() {
        return custmer;
    }
    public void setCustmer(String custmer) {
        this.custmer = custmer;
    }
     
    @XmlElement
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
     
    @XmlElement(name = "bill-amount")
    public String getAmount() {
        return amount;
    }
    public void setAmount(String amount) {
        this.amount = amount;
    }    
   }

OrderResponse.java

    @XmlRootElement(name = "orderResponse")
    public class OrderResponse{    
    private String response;

    @XmlElement(name = "response")
    public String getResponse() {
        return response;
    }
    public void setResponse(String response) {
        this.response= response;
    }
    }

XML Request:

<order>
    <order-no></order-no>
    <custmer></custmer>
    <address></address>
    <bill-amount></bill-amount>
</order>

XML Response:

<orderResponse>
    <response></response>
</orderResponse>

Mainly a Rest API is based on JSON documents, and is usually the main standard for most implementations. Even so, nothing prevents you from sending the application/json content type to your HTTP server instead of sending application/xml and making an API processing it that way.

To process messages encoded in the SOAP subset of XML, you'll need to retrieve the request body text and process it through some kind of SOAP message processing engine. For the return message, you will have to work in the same way, processing the type of message you want, obtaining the XML and returning it to the client.

    @Path("/processOrder")
    @POST
    @Produces("application/xml")
    @Consumes("application/xml")
    public String processOrder(String order) {
        String result = "";
        /* Some kind of the logic to process SOAP Messages. */
        return result;
    }

Finally, a library that I can recommend so you can implement this logic is Jakarta SOAP, from here you can process the SOAP messages you get from the request.

Link to library: https://eclipse-ee4j.github.io/metro-saaj/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM