这是我在网络应用程序上获得的一个 Controller :
@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();
}
这个 Controller 是通过ajax使用post调用并传递一个json,并且jackson映射器负责它作为POJO到达(很好!)
我现在想做的是: 在另一个 Web 应用程序中,我想使用 http post 请求进行调用,将 PlayerAccountDTO 传递到这个确切的 Controller ,当然也会收到 ResponseDTO。
我希望尽可能简单。
有可能实现吗?这是我如意的解决方案(不同网络应用程序上的服务):
public ResponseDTO createAccountOnADifferentWebApp() {
PlayerAccountDTO dto = new PlayerAccountDTO(...);
ResponseDTO result = httpRequestPost(url, dto, ResponseDTO.class);
return result;
}
最佳答案
您的网络服务器未收到 PlayerAccountDTO
对象。它接收一个 HTTP 请求,其正文(可能)包含 JSON 对象。 Spring Web 应用程序尝试将该 JSON 反序列化为一个 PlayerAccountDTO
对象,并将其传递给您的处理程序方法。
因此,您想要做的是使用 HTTP 客户端,它将客户端上的 PlayerAcountDTO
序列化为您在 HTTP 请求中发送的一些 JSON。
查看RestTemplate
它是一个 Spring HTTP 客户端,并使用与 Spring 在 @ResponseBody
注解方法和 @RequestBody
注解中序列化和反序列化对象相同的 HttpMessageConverter
对象参数。
关于java - Spring MVC - 使用 java 为 Controller 创建 httprequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21289217/