Home > Software engineering >  Getting null values as response in restTemplate call
Getting null values as response in restTemplate call

Time:12-12

I am making a resttemplate call in springboot application . Getting data in response only in 'status' field . The parsed object 'payload' is having all value as null

{ 
   "status": "OK",
    "payload": {
        "Chances": null,
        "PlainOutput": null,
        "Email": null,
        "Display": null,
        "FromSystem": null
    }
}

Below is the response from Postman. It is having all the data. I tried the same in browser and getting similar response. It is an http call.

{
    "status": "OK",
    "payload": {
        "Chances": "verified",
        "PlainOutput": "allOk",
        "Email": "[email protected]",
        "Display": "OK",
        "FromSystem": "N"
    }
}

Below is the restTemplate call:

ResponseEntity<ServiceResponse> responseEntity = restTemplate.exchange(uriComponents.toUriString(),
                httpMethod,  requestEntity, ServiceResponse.class);

The POJO is as below :

public class ServiceResponse implements Serializable {

    private static final long serialVersionUID = 1L;

    public ServiceResponse(String status, ServicePayload payload) {
        this.status = status;
        this.payload = payload;
    }

    public ServiceResponse(){

    }

    private String status;

    @JsonProperty("payload")
    private ServicePayload payload;

    public String getStatus() {
        return status;
    }


    public ServicePayload getPayload() {
        return payload;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setPayload(ServicePayload payload) {
        this.payload = payload;
    }

    @Override
    public String toString() {
        return "ServicePayload{"  
                "status='"   status   '\''  
                ", payload="   payload  
                '}';
    }
}

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonRootName(value = "payload")
public class ServicePayload implements Serializable {

    private static final long serialVersionUID = 1L;

    public ServicePayload(){

    }
    public ServicePayload(String chances, String plainOutput, String email, String display, String fromLocal) {
        this.certainty = certainty;
        this.plainOutput = plainOutput;
        this.email = email;
        this.display = display;
        this.fromLocal = fromLocal;
    }

    @JsonProperty("Chances")
    private String chances;

    @JsonProperty("PlainOutput")
    private String plainOutput;

    @JsonProperty("Email")
    private String email;

    @JsonProperty("Display")
    private String display;

    @JsonProperty("FromSystem")
    private String fromSystem;

    public void setChances(String chances) {
        this.chances = chances;
    }

    public void setPlainOutput(String plainOutput) {
        this.plainOutput = plainOutput;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setMessage(String display) {
        this.display = display;
    }

    public void setFromSystem(String fromSystem) {
        this.fromSystem = fromSystem;
    }

    public String getChances() {
        return chances;
    }

    public String getVerboseOutput() {
        return plainOutput;
    }

    public String getEmail() {
        return email;
    }

    public String getMessage() {
        return message;
    }

    public String getFromSystem() {
        return fromSystem;
    }

    @Override
    public String toString() {
        return "ServicePayload{"  
                "chances='"   chances   '\''  
                ", plainOutput='"   plainOutput   '\''  
                ", email='"   email   '\''  
                ", display='"   display   '\''  
                ", fromSystem='"   fromSystem   '\''  
                '}';
    }
}

Can you please help what I am doing wrong here.

CodePudding user response:

I have tested the code for RestTemplate#exchange(URI, HttpMethod, HttpEntity, Class) with the classes you are expecting and it seems to be working fine with default configurations from Spring. Maybe there are specific configurations that are changing the default behaviour of the jackson mappings?

@GetMapping("/call-endpoint")
public ServiceResponse callEndpoint(){
    ResponseEntity<ServiceResponse> response = restTemplate.exchange(
            "http://localhost:8080/get-payload",
            HttpMethod.GET,
            null,
            ServiceResponse.class
    );

    if(response.getBody() != null && response.getBody().getPayload() != null) {
        log.info("Email from request: {}", response.getBody().getPayload().getEmail());
    } else {
        log.error("Payload is null");
    }

    return response.getBody();
}

Link to GitHub for the above code.

CodePudding user response:

I have test your code , some error , like message filed. others is ok,here is my test code.

public class Test {
    public static void main(String[] args) throws JsonProcessingException {
        String jj = "{\n"  
                "    \"status\": \"OK\",\n"  
                "    \"payload\": {\n"  
                "        \"Chances\": \"verified\",\n"  
                "        \"PlainOutput\": \"allOk\",\n"  
                "        \"Email\": \"[email protected]\",\n"  
                "        \"Display\": \"OK\",\n"  
                "        \"FromSystem\": \"N\"\n"  
                "    }\n"  
                "}";

        ObjectMapper objectMapper = new ObjectMapper();
        final ServiceResponse serviceResponse = objectMapper.readValue(jj, ServiceResponse.class);
        System.out.println(1);
        System.out.println(objectMapper.writeValueAsString(serviceResponse));
    }
}

so if your return value is corrent , Debug HttpMessageConverterExtractor#extractData might help you. if data is ok. i guess problem at jackson's deserialization. maybe jackson not work. it's depend on debug result.

  • Related