英文:
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.
{
"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.
答案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 = "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"]
}
答案2
得分: 0
{
"empID": [
{
"value": "111"
},
{
"value": "222"
}
]
}
英文:
try with this json request.
{
"empID": [
{
"value": "111"
},
{
"value": "222"
}
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论