我正在尝试使用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;

最佳答案

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

由于返回字段是IdBusiness,并且请求期望属性idbusiness,因此抛出异常,我不得不将idbusiness json属性重命名为IdBusiness,其他所有属性都重命名。

10-08 20:00