RestTemplate postforobject 在尝试 post 自定义对象时导致错误
RestTemplate postforobject causes an error when trying to post to a custom object
我正在尝试使用 postforobject 检索对象,只有当我尝试检索到自定义对象时才会收到异常错误,如果我重写服务和 return 单个字符串并将 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;
这是正事 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;
无法读取 JSON:无法识别字段 "IdBusiness",未标记为可忽略(5 个已知属性:"imagelogo"、"idbusiness"、"password"、"name","cellphone"])
因为 return 字段是 IdBusiness 并且请求期望 属性 idbusiness 它抛出一个异常我不得不将 idbusiness json 属性 重命名为 IdBusiness 并且相同所有其他属性。
我正在尝试使用 postforobject 检索对象,只有当我尝试检索到自定义对象时才会收到异常错误,如果我重写服务和 return 单个字符串并将 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;
这是正事 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;
无法读取 JSON:无法识别字段 "IdBusiness",未标记为可忽略(5 个已知属性:"imagelogo"、"idbusiness"、"password"、"name","cellphone"])
因为 return 字段是 IdBusiness 并且请求期望 属性 idbusiness 它抛出一个异常我不得不将 idbusiness json 属性 重命名为 IdBusiness 并且相同所有其他属性。