gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:34:58 24 4
gpt4 key购买 nike

我正在尝试使用 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,对于所有其他属性也是如此。

关于java - RestTemplate postforobject 在尝试发布到自定义对象时会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49008306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com