Restful Spring postForObject 缺少所有 属性 值
Restful Spring postForObject missing all property values
我尝试模拟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) ;
}
控制器代码:
@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 和 country 也为 null..
我的代码有什么问题?
您正在控制器中使用 @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);
我尝试模拟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) ;
}
控制器代码:
@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 和 country 也为 null.. 我的代码有什么问题?
您正在控制器中使用 @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);