2015-10-15 50 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如果您有新问题,请提出新问题。 –