gpt4 book ai didi

java - Restful Spring postForObject 缺少所有属性值

转载 作者:行者123 更新时间:2023-11-30 07:48:56 25 4
gpt4 key购买 nike

我尝试模拟 Restful 服务器:

private void btnPostActionPerformed(java.awt.event.ActionEvent evt) {                                        
RestTemplate restTemplate = new RestTemplate();

Issuer issuer = new Issuer();
issuer.setCountry("Teacher 1");
issuer.setIssuerName("Department 1");
String url = txtHost.getText()+txtGet.getText();
restTemplate.postForObject(url, issuer, Issuer.class) ;
}

Controller 代码:

 @RequestMapping(value = "/issuer/addIssuer", method = RequestMethod.POST)
@ResponseBody
public Issuer addIssuer(@ModelAttribute("issuer") Issuer issuer) {

if (issuer != null) {
logger.info("Inside addIssuer, adding: " + issuer.toString());
} else {
logger.info("Inside addIssuer...");
}
issuers.put(issuer.getTicker(), issuer);
return issuer;

}

我已经填充了一些属性,但是当我调试服务器时,所有值都是空。

信息:com.avaldes.tutorial.RestController - 在 addIssuer 内部,添加:[null, null, null, null]

IssuerName 和国家/地区也为空..我的代码有什么问题吗?

最佳答案

您正在 Controller 中使用@ModelAttribute。在这种情况下,您需要将数据发送为 application/x-www-form-urlencoded:

MultiValueMap<String, Object> variables = new LinkedMultiValueMap<>();
variables.add("country", "Teacher 1");
variables.add("issuerName", "Department 1");

RestTemplate restTemplate = new RestTemplate();

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(variables, requestHeaders);

String url = txtHost.getText()+txtGet.getText();
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Issuer.class);

关于java - Restful Spring postForObject 缺少所有属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33541126/

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