2015-10-15 57 views
0

所以我有了這個控制器方法,這顯然是返回ResponseEntity:Java - RestTemplate語法錯誤?

@RequestMapping(method=RequestMethod.POST, value="sendEmail") 
    public ResponseEntity sendPasswordResetEmail (@RequestParam("name")  final String name, 
             @RequestParam("password") final String password, 
             @RequestParam("email") final String email) 
{ 
      final boolean success = notificationService.sendPasswordResetEmail(name, password, email); 
      return success ? 
        new ResponseEntity(HttpStatus.OK) : new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR); 
} 

我的問題是,調用以下時,它沒有讓我設置ResponseEntity.class作爲第三個參數,它不沒有道理,因爲這是預期的返回類型:

ResponseEntity<String> auth = restTemplate.postForEntity(url, entity, ResponseEntity.class); 

任何提示?

UPDATE:

這怎麼可能,代碼可以編譯和運行,我得到這個消費終端是什麼時候?

{ 
    "timestamp": 1444927133682, 
    "status": 500, 
    "error": "Internal Server Error", 
    "exception": "java.lang.NoClassDefFoundError", 
    "message": "org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: com/api/model/postmark/ResetPassword", 
    "path": "/v1/api/notify/sendPasswordResetEmail" 
} 

回答

1

postForEntity方法,你正試圖使用​​的簽名是

public <T> ResponseEntity<T> postForEntity(URI url, 
             Object request, 
             Class<T> responseType) 
           throws RestClientException 

換句話說,它總是會返回一個ResponseEntity。您提供的參數是響應主體的類型,它將告訴RestTemplate如何反序列化內容。

只需使用

ResponseEntity<String> auth = restTemplate.postForEntity(url, entity, String.class); 
+0

感謝的人。仍然沒有工作(這是扔我一個400 BAD REQUEST),但至少語法錯誤消失了。 –

+0

這就是我得到:「{ 「時間戳」:1444927133682, 「狀態」:500, 「錯誤」: 「內部服務器錯誤」, 「異常」: 「java.lang.NoClassDefFoundError」, 「消息「:」org.springframework.web.util.NestedServletException:處理程序處理失敗;嵌套的異常是java.lang.NoClassDefFoundError:com/thortful/api/model/postmark/ResetPassword「, 」path「:」/ v1/api/notify/sendPasswordResetEmail「 }' –

+0

@stackpepe如果您有新問題,請提出新問題。 –