尝试发布到自定义对象时,RestTemplate postforobject会导致错误

时间:2018-02-27 12:05:46

标签: java android restlet

我试图用postforobject检索一个对象,只有当我尝试检索自定义对象时才会出现异常错误,如果我重写服务并返回一个字符串并将postforobject返回到一个字符串就可以了

不起作用的代码:

 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;

正在运行的代码:

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;

这是商务舱:

  @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;

1 个答案:

答案 0 :(得分:1)

无法读取JSON:无法识别的字段“IdBusiness”,未标记为可忽略(5个已知属性:“imagelogo”,“idbusiness”,“password”,“name”,“mobile”])

因为返回字段是IdBusiness,并且请求期望属性idbusiness,所以它抛出异常我必须将idbusiness json属性重命名为IdBusiness,并且对所有其他属性都重命名。