从SpringBoot消费SOAP服务 – @RequestBody获取null值

huangapple go评论19阅读模式
英文:

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.

我在调用Rest服务时,发现在RequestControllers的RequestBody MyReq请求中,empID的值为null。使用以下JSON请求时出现的问题。

{
   "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

我的SoapClient类

@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

从SOAP服务的WSDL生成的JAXB MyReq和EmpID类

@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

生成的EmpID类

@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.

我尝试在JSON请求中使用了empID,但仍然获取到了null值。

英文:

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

{
   &quot;EmpID&quot;: [
      &quot;1111&quot;,&quot;1234&quot;
   ]
}

This is my Controller

    @SpringBootApplication
        @RestController
        public class MessageProcessorApplication {
        @Autowired
        	private SoapClient client;
        	
        	@RequestMapping(value = &quot;/getIdDetails&quot;, 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(&quot;http://localhost:8080/ws&quot;,request);
		return response;
	}
	}

jaxb generated MyReq and EmpID classes from SOAP Service WSDL

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

generated EmpID class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = &quot;&quot;, propOrder = {
    &quot;value&quot;
})
@XmlRootElement(name = &quot;EmpID&quot;)
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.

答案1

得分: 1

以下是翻译好的内容:

你可能遇到了这个问题。你还需要在构造函数中传递EmpID的值。

如果我将你生成的类更改为以下内容,我可以让你的示例运行...

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;
    }
}

然后,你需要将你的 JSON 提交为...

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

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 = &quot;MyReq&quot;)
public class MyReq
{
    @XmlElement(required = true)
    protected List&lt;EmpID&gt; empIds;

    public List&lt;EmpID&gt; getEmpIds() {
        return empIds;
    }

    public void setEmpIds(List&lt;EmpID&gt; 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...

{
    &quot;empIds&quot;: [&quot;1111&quot;,&quot;1234&quot;]
}

答案2

得分: 0

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

英文:

try with this json request.

{
    &quot;empID&quot;: [
        {
            &quot;value&quot;: &quot;111&quot;
        },
        {
            &quot;value&quot;: &quot;222&quot;
        }
    ]
}

huangapple
  • 本文由 发表于 2020年8月3日 08:47:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63222382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定