Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonarQube complains about using ResponseEntity with a wildcard

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?

like image 853
Ilya Zinkovich Avatar asked Nov 19 '22 16:11

Ilya Zinkovich


1 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);
    }
like image 164
Alexander Golovinov Avatar answered Jun 18 '23 16:06

Alexander Golovinov