Cannot infer type arguments for ResponseEntity<>
Asked Answered
C

3

7

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?

Contraceptive answered 26/6, 2019 at 20:3 Comment(5)
is that whole error message? can you add more details? thanksHouselights
Use ResponseEntity.ok(value). Additionally, it's more conventional to throw an exception in the first case and gave a global exception handler build error responses for consistency.Pyretotherapy
As pointed out, it much better to handle errors in a ControllerAdvice so google that. Just wanted to say your method is returning a ResponseEntity<?> With means a response type containing "any" type. When you declare your responses you set the diamond operator to <> nothing. You are basically telling to compiler to figure out the return type but it can't so you get Cannot infer type arguments for ResponseEntity<>. Why can't it figure it out, well the compiler will look at the method return type to figure it out but you have declared it to be any type. So the compiler cant guess.Airtight
the quick and ugly fix would be to declare ResponseEntity<String> and ResponseEntity< NotificationEchoResponse> but this is an ugly solution.Airtight
Can you show me code example, please?Contraceptive
P
7

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);
   }
Parthenopaeus answered 28/6, 2019 at 19:9 Comment(0)
S
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);
}

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

Stickinthemud answered 5/3, 2020 at 6:45 Comment(0)
Z
0

The return type of ResponseEntity should be <String>. As you are returning this string: "Please contact technical support!"

Or you can simply do ResponseEntity<Object>

Zingaro answered 4/1, 2022 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.