Spring MVC的 - 的控制器
问题描述:
在这里创建了Java一个HttpRequest是一个控制器我在我的web应用程序有:Spring MVC的 - 的控制器
@RequestMapping(value = "/createAccount", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public ResponseDTO createAccount(@RequestBody PlayerAccountDTO playerAccountDTO,
HttpServletRequest request) {
this.playerService.createAccount(playerAccountDTO);
return new ResponseDTO();
}
该控制器通过AJAX使用后,并传递一个JSON和杰克逊映射照顾被称为作为POJO(Nice!)
我现在想要做的是: 在另一个web应用程序中,我想用http post请求传递PlayerAccountDTO给这个确切的控制器,并且当然会收到ResponseDTO 。
我希望尽可能简单。
是否有可能实现这一目标?这里是我的wishfull溶液(在不同的Web应用程序服务):
public ResponseDTO createAccountOnADifferentWebApp() {
PlayerAccountDTO dto = new PlayerAccountDTO(...);
ResponseDTO result = httpRequestPost(url, dto, ResponseDTO.class);
return result;
}
答
您的Web服务器没有收到PlayerAccountDTO
对象做到这一点。它接收到一个包含JSON对象(可能包含一个)的HTTP请求。 Spring Web应用程序尝试将该JSON反序列化为传递给您的处理程序方法的PlayerAccountDTO
对象。
所以你想要做的是使用HTTP客户端,它将客户端的PlayerAcocuntDTO
串行化为您在HTTP请求中发送的某个JSON。
退房RestTemplate
这是一个春天HTTP客户端,并使用了Spring使用序列化和@ResponseBody
注释的方法和@RequestBody
标注的参数反序列化对象相同HttpMessageConverter
对象。