Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot infer type arguments for ResponseEntity<>

I want to implement Spring endpoint in which I can return XML object NotificationEchoResponse and http status code. I tried this:

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

But I get error: Cannot infer type arguments for ResponseEntity<> at this line: return new ResponseEntity<>("Please contact technical support!", HttpStatus.INTERNAL_SERVER_ERROR); Do you know know how I can fix this issue?

like image 207
Peter Penzov Avatar asked Jun 26 '19 20:06

Peter Penzov


2 Answers

You can use the

    ResponseEntity<Object> 

like that

OR

You can make you own custom class like ResponseData and in that class put a field like paylod

  public class ResponseData {
      private Object payload;
   } 

and use like that ResponseEntity and set that value.

Now your controller will look like that

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

You can replace the response data with Object also , then the

  @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);
   }
like image 163
Ashwani Tiwari Avatar answered Oct 20 '22 17:10

Ashwani Tiwari


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

in this code return type of "productPrefixService.deleteProductPrefix(inputFields)" must be ResponseEntity.

like image 2
alok Avatar answered Oct 20 '22 18:10

alok