shiva tatikonda
shiva tatikonda

Reputation: 101

Consume SOAP Service from SpringBoot - @RequestBody getting null values

I am getting null value for empID in RequestControllers RequestBody MyReq request. When I called Rest Service using below JSON Request.

{
   "EmpID": [
      "1111","1234"
   ]
}

This is my Controller

    @SpringBootApplication
        @RestController
        public class MessageProcessorApplication {
        @Autowired
            private SoapClient client;
            
            @RequestMapping(value = "/getIdDetails", method = RequestMethod.POST)
            public MyRsp invokeSoapClient(@RequestBody MyReq request)
            {
                
                return client.getIdDetails(request);
            }
        
            }

My SoapClient class

@Service
    public class SoapClient {
    @Autowired
    private Jaxb2Marshaller marshaller;

    private WebServiceTemplate template;
    
    public MyRsp getIdDetails(MyReq request)
    {
        template = new WebServiceTemplate(marshaller);
        MyRsp response = (MyRsp) template.marshalSendAndReceive("http://localhost:8080/ws",request);
        return response;
    }
    }

jaxb generated MyReq and EmpID classes from SOAP Service WSDL

@XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "empID"
    })
    @XmlRootElement(name = "MyReq")
    public class MyReq
        extends BaseReq
    {
    
        @XmlElement(name = "EmpID", required = true)
        protected List<EmpID> empID;
    
        public void setEmpID(List<EmpID> empID) {
            this.empID = empID;
        }
        public List<EmpID> getEmpID() {
            if (empID == null) {
                empID = new ArrayList<EmpID>();
            }
            return this.empID;
        }
    
    }
    
    }

generated EmpID class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "value"
})
@XmlRootElement(name = "EmpID")
public class EmpID {

    @XmlValue
    protected String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

I have tried with empID also in JSON Request. Still null values I am getting.

Upvotes: 1

Views: 1409

Answers (2)

robleney
robleney

Reputation: 86

You may be running into this problem. You also need to pass the value of EmpID in the constructor.

I can get your example to work if I change your generated classes to...

MyReq.java

package com.example.demo;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "MyReq")
public class MyReq
{
    @XmlElement(required = true)
    protected List<EmpID> empIds;

    public List<EmpID> getEmpIds() {
        return empIds;
    }

    public void setEmpIds(List<EmpID> empIds) {
        this.empIds = empIds;
    }
}

EmpID.java

package com.example.demo;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlValue;

@XmlAccessorType(XmlAccessType.FIELD)
public class EmpID {

    public EmpID(String value) {
        this.value = value;
    }

    @XmlValue
    protected String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

You would then need to post your json as...

{
    "empIds": ["1111","1234"]
}

Upvotes: 1

Holinc
Holinc

Reputation: 723

try with this json request.

{
    "empID": [
        {
            "value": "111"
        },
        {
            "value": "222"
        }
    ]
}

Upvotes: 0

Related Questions