【问题标题】:Cannot infer type arguments for ResponseEntity<>无法推断 ResponseEntity<> 的类型参数
【发布时间】: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&lt;&gt; 在这一行:return new ResponseEntity&lt;&gt;("Please contact technical support!", HttpStatus.INTERNAL_SERVER_ERROR); 你知道我该如何解决这个问题吗?

【问题讨论】:

  • 是整个错误信息吗?你能添加更多细节吗?谢谢
  • 使用ResponseEntity.ok(value)。此外,更传统的做法是在第一种情况下抛出异常,并为全局异常处理程序构建错误响应以保持一致性。
  • 正如所指出的,处理ControllerAdvice 中的错误要好得多,所以谷歌一下。只是想说您的方法返回 ResponseEntity&lt;?&gt; With 表示包含“任何”类型的响应类型。当您声明您的响应时,您将菱形运算符设置为 &lt;&gt; 什么都没有。你基本上是在告诉编译器找出返回类型,但它不能,所以你得到Cannot infer type arguments for ResponseEntity&lt;&gt;。为什么它不能弄明白,编译器会查看方法返回类型来弄明白,但您已将其声明为任何类型。所以编译器无法猜测。
  • 快速而丑陋的解决方法是声明 ResponseEntity&lt;String&gt;ResponseEntity&lt; NotificationEchoResponse&gt; 但这是一个丑陋的解决方案。
  • 你能给我看一下代码示例吗?

标签: java spring spring-boot spring-mvc


【解决方案1】:

你可以使用

    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);
   }

【讨论】:

    【解决方案2】:
    public ResponseEntity<BasicCreateUpdateResponse> deleteProductPrefix(@RequestBody ProductPrefixEntity inputFields) {
        if(inputFields.getRecid().isEmpty()) {
            throw new ApplicationException(NPIConstants.ERR_500, HttpStatus.OK, null, null);
        }
        logger.info("Inside resources updateProduct method() .........");
        return new ResponseEntity<>(productPrefixService.deleteProductPrefix(inputFields),HttpStatus.OK);
    }
    

    在此代码中“productPrefixService.deleteProductPrefix(inputFields)”的返回类型必须是ResponseEntity。

    【讨论】:

      【解决方案3】:

      ResponseEntity 的返回类型应该是&lt;String&gt;。 当您返回此字符串时:“请联系技术支持!”

      或者你可以简单地做ResponseEntity&lt;Object&gt;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-07
        • 1970-01-01
        • 1970-01-01
        • 2021-04-06
        • 2014-08-07
        相关资源
        最近更新 更多