Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch xml through request body and parse to Spring boot

I am trying to fetch xml through postman into spring boot rest api. But having a problem while fetching the data from it. How to parse it into spring boot application and get the attributes values. Otherwise, How to fetch xml data by creating pojo class.

Following is the xml file:

<Transmission>
    <TransmissionHeader/>
    <TransmissionBody>
        <GLogXMLElement>
            <TransmissionReport>
                <Name>FUEL</Name>
                <Number>57</Number>
                <Status>PROCESSED</Status>
                <TransmissionSummary>
                    <FirstTransactionNo>1017</FirstTransactionNo>
                    <LastTransactionNo>1017</LastTransactionNo>
                </TransmissionSummary>
            </TransmissionReport>
        </GLogXMLElement>
    </TransmissionBody>
</Transmission>

Thanks in advance.

like image 737
Nikhil Katore Avatar asked Dec 05 '25 14:12

Nikhil Katore


1 Answers

In Spring boot, to get the request body -

@RequestMapping(method = RequestMethod.POST)
void testEndPoint(@PathVariable String param, @RequestBody String xml) {
//do stuff
}

See below for details -

Spring Boot Guide

@RequestBody

In order to convert it to POJO, you could explore using Xstream - XStream Tutorial

Define your POJOs and you can simply do -

POJO pojo = (POJO)xstream.fromXML(xml);
like image 123
a_m Avatar answered Dec 07 '25 05:12

a_m



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!