簡體   English   中英

無法推斷 ResponseEntity<> 的類型參數

[英]Cannot infer type arguments for 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<>在這一行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);
   }
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。

ResponseEntity 的返回類型應該是<String> 當您返回此字符串時:“請聯系技術支持!”

或者你可以簡單地做ResponseEntity<Object>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM