SonarQube complains about using ResponseEntity with a wildcard

Viewed 7862

I use SpringBoot for REST web services development and SonarQube for static analysis.

I have a few endpoints in my application that look the following way:

@PostMapping
ResponseEntity<?> addSomething(@RequestBody Some object) {
    // some code there
    return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

SonarQube complains about using ResponseEntity with a wildcard, reporting me a Critical issue "Generic wildcard types should not be used in return parameters".

I wonder if I should disable this verification in SonarQube or come up with something different for return type for these cases.

What do you think about it?

3 Answers
@PostMapping
ResponseEntity<Object> addSomething(@RequestBody Some object) {
    // some code there
    return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

This will also remove the error. It is still very generic, but it is one of the solutions if you want to return different types based on the outcome. For instance:

@PostMapping
    ResponseEntity<Object> addSomething(@RequestBody Some object) {

        //Will return ResponseEntity<> with errors
        ResponseEntity<Object> errors = mapValidationService(bindingResult);
        if (!ObjectUtils.isEmpty(errors)) return errors;
        
        // some code there
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
Related