0

I have a controller with the below mentioned contract ---

  @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();
    }

Other than this there are two pojo classes --

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 + "]";
    }

}

Now I am using spring boot and have added jackson-data-bind dependency for content negotiation, also I wanted to consume both json as well as xml data as an input and thus mapping it to the POJO file.

but I am not able to attain the desired result, even I am getting below mentioned error in the logs when trying to send across json from a rest client.

Error----

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 send across --

    "certificate": {
    "base64String": "abc"
  }

Please also let me know whether it will work properly for xml payload as well, as I want to consume both xml as well as json input

  • see this http://stackoverflow.com/questions/5908466/jquery-spring-mvc-requestbody-and-json-making-it-work-together/5908632#5908632 – Suraj Feb 07 '17 at 08:49

2 Answers2

0
  1. Show us how do You make a request and double check the names of variables.
  2. Check and recheck if You have the correct IMPORTS in the controller, if the Certificate is actually from Your package and not any other.
  3. Add @JsonInclude(Include.NON_NULL) class Foo{} so You won`t have null problems.
  4. Delete for testing the serialVersionUID from certificate.
  5. Try to add @ResponseBody to You consuming controller method.
  6. Try to send

    { "base64String": "abc" }

without the variable name.

0

I worked by creating parametrised constructor in the POJO class, seems it jackson data bind requires a parametrised constructor for object creation.

Still have to check for xml input though.