【发布时间】:2019-06-26 20:03:41
【问题描述】:
我想实现 Spring 端点,我可以在其中返回 XML 对象 NotificationEchoResponse 和 http 状态代码。我试过这个:
@PostMapping(value = "/v1/notification", produces = "application/xml")
public ResponseEntity<?> handleNotifications(@RequestParam MultiValueMap<String, Object> keyValuePairs) {
if (!tnx_sirnature.equals(signature))
{
return new ResponseEntity<>("Please contact technical support!", HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<>(new NotificationEchoResponse(unique_id), HttpStatus.OK);
}
但我收到错误:Cannot infer type arguments for ResponseEntity<> 在这一行:return new ResponseEntity<>("Please contact technical support!", HttpStatus.INTERNAL_SERVER_ERROR); 你知道我该如何解决这个问题吗?
【问题讨论】:
-
是整个错误信息吗?你能添加更多细节吗?谢谢
-
使用
ResponseEntity.ok(value)。此外,更传统的做法是在第一种情况下抛出异常,并为全局异常处理程序构建错误响应以保持一致性。 -
正如所指出的,处理
ControllerAdvice中的错误要好得多,所以谷歌一下。只是想说您的方法返回ResponseEntity<?>With 表示包含“任何”类型的响应类型。当您声明您的响应时,您将菱形运算符设置为<>什么都没有。你基本上是在告诉编译器找出返回类型,但它不能,所以你得到Cannot infer type arguments for ResponseEntity<>。为什么它不能弄明白,编译器会查看方法返回类型来弄明白,但您已将其声明为任何类型。所以编译器无法猜测。 -
快速而丑陋的解决方法是声明
ResponseEntity<String>和ResponseEntity< NotificationEchoResponse>但这是一个丑陋的解决方案。 -
你能给我看一下代码示例吗?
标签: java spring spring-boot spring-mvc