所以我有这个 Controller 方法,它显然返回一个 ResponseEntity:
@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);
有什么提示吗?
更新:
代码如何编译并运行并且我在使用端点时得到这个?
{
"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"
}
最佳答案
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);
关于Java - RestTemplate 语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33153229/