java - 无法推断 ResponseEntity<> 的类型参数
问题描述
我想实现 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<Object>
像那样
或者
您可以创建自己的自定义类,如 ResponseData 并在该类中放置一个字段,如 paylod
public class ResponseData {
private Object payload;
}
并像那样使用 ResponseEntity 并设置该值。
现在你的控制器看起来像这样
@PostMapping(value = "/v1/notification", produces = "application/xml")
public ResponseEntity<ResponseData> handleNotifications(@RequestParam
MultiValueMap<String, Object> keyValuePairs) {
if (!tnx_sirnature.equals(signature))
{
return new ResponseEntity<ResponseData>(new ResponseData("Please contact to technical support"),
HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<ResponseData>(new ResponseData(new NotificationEchoResponse(unique_id)),
HttpStatus.OK);
}
您也可以用 Object 替换响应数据,然后
@PostMapping(value = "/v1/notification", produces = "application/xml")
public ResponseEntity<Object> handleNotifications(@RequestParam
MultiValueMap<String, Object> keyValuePairs) {
if (!tnx_sirnature.equals(signature))
{
return new ResponseEntity<Object>("Please contact to technical support",
HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<Object>(new NotificationEchoResponse(unique_id),
HttpStatus.OK);
}
推荐阅读
- java - 如何使用 Selenium 和 Java 定位并单击选择布尔复选框
- ios - UITesting 只是失败了,没有更多的上下文就没有编写测试
- c++ - 结果不是我所期望的
- linux - Gentoo中没有无线连接
- c++ - 安装斯坦福 C++ 库(图形、BasicGraph 类)时遇到问题
- regex - 正则表达式如何匹配 dd mmm yyyy 以仅选择年份的前两位数字
- google-maps - Pass Pins values to next page
- json - How to get the JSON of the button payloads in Rasa and display them on the User Interface
- python - 如何在自定义 tf.keras.layers.Layer 中支持遮罩
- shell - use perl to repeat a line fixed no. of times