我有一个控制器,下面提到的合同---
@RequestMapping(value="/api/devices/certs",method = RequestMethod.POST,consumes={"application/json","application/xml"})
public String submitCertificate(@RequestBody Certificate certificate){
System.out.println(certificate.getBase64String());
return certificate.getBase64String();
}
除此之外还有两个pojo类 -
1)
public class DeviceCertificateRequest implements Serializable {
private static final long serialVersionUID = -4408117936126030294L;
private Certificate certificate;
public Certificate getCertificate() {
return certificate;
}
public void setCertificate(Certificate certificate) {
this.certificate = certificate;
}
@Override
public String toString() {
return "DeviceCertifficateRequest [certificate=" + certificate + "]";
}
}
2)
public class Certificate implements Serializable {
private static final long serialVersionUID = 4044105355620137636L;
private String base64String;
public String getBase64String() {
return base64String;
}
public void setBase64String(String base64String) {
this.base64String = base64String;
}
@Override
public String toString() {
return "Certificate [base64String=" + base64String + "]";
}
}
现在我使用spring boot并为内容协商添加了jackson-data-bind依赖项,我也希望将json和xml数据作为输入使用,从而将其映射到POJO文件。
但是我无法达到预期的结果,即使我在尝试从其他客户端发送json时在日志中遇到下面提到的错误。
错误----
ERROR] 2017-02-07 13:48:45.448 [http-nio-8080-exec-1] ConfigManagerExceptionHandler - exception while accessing url:-http://localhost:8080/api/devices/certserror message:-Could not read document: Can not construct instance of com.lufthansa.configmanager.request.beans.Certificate: no String-argument constructor/factory method to deserialize from String value ('DeviceCertificateRequest')
at [Source: java.io.PushbackInputStream@3c891128; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.lufthansa.configmanager.request.beans.Certificate: no String-argument constructor/factory method to deserialize from String value ('DeviceCertificateRequest')
at [Source: java.io.PushbackInputStream@3c891128; line: 1, column: 1]
Json发送 -
"certificate": {
"base64String": "abc"
}
请告诉我它是否也适用于xml有效负载,因为我想同时使用xml和json输入
答案 0 :(得分:0)
尝试发送
{ “base64String”:“abc” }
没有变量名。
答案 1 :(得分:0)
我在POJO类中创建参数化构造函数,似乎jackson数据绑定需要参数化构造函数来创建对象。
仍然需要检查xml输入。