Search code examples
javaandroidrestlet

RestTemplate postforobject causes an error when trying to post to a custom object


I'm trying to retrieve an object with postforobject, I get an exception error only if I try to retrieve to a custom object, if I rewrite the service and return a single string and postforobject to a string it works fine.

code that isn't working :

 MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
            headers.add("Content-Type","application/json");
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            Business business = new Business();
            business.setName(et_name.getText().toString());
            business.setPassword(et_password.getText().toString());
            HttpEntity<Business> request = new HttpEntity<Business>(business,headers);
            Business response =  restTemplate.postForObject(url,request,Business.class);
            return response;

code that is working:

MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
            headers.add("Content-Type","application/json");
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            Business business = new Business();
            business.setName(et_name.getText().toString());
            business.setPassword(et_password.getText().toString());
            HttpEntity<String> request = new HttpEntity<String>(business,headers);
            String response =  restTemplate.postForObject(url,request,String.class);
            return response;

and here's the business class :

  @JsonProperty("idbusiness")
private int idbusiness;
@JsonProperty("name")
private String name;
@JsonProperty("password")
private String password;
@JsonProperty("cellphone")
private String cellphone;
@JsonProperty("imagelogo")
private String imagelogo;

Solution

  • Could not read JSON: Unrecognized field "IdBusiness" , not marked as ignorable (5 known properties: "imagelogo","idbusiness","password","name","cellphone"])

    since the return field is IdBusiness and the request expects property idbusiness it throws an exception I had to rename the idbusiness json property to IdBusiness and same for all other properties.