2014-01-22 184 views
0

在這裏創建了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; 
} 

回答

1

您的Web服務器沒有收到PlayerAccountDTO對象做到這一點。它接收到一個包含JSON對象(可能包含一個)的HTTP請求。 Spring Web應用程序嘗試將該JSON反序列化爲傳遞給您的處理程序方法的PlayerAccountDTO對象。

所以你想要做的是使用HTTP客戶端,它將客戶端的PlayerAcocuntDTO串行化爲您在HTTP請求中發送的某個JSON。

退房RestTemplate這是一個春天HTTP客戶端,並使用了Spring使用序列化和@ResponseBody註釋的方法和@RequestBody標註的參數反序列化對象相同HttpMessageConverter對象。