Ily*_*ich 5 spring spring-mvc spring-boot sonarqube
我已经看到很多 Spring Controllers 实现的例子,它们用于ResponseEntity<?>返回具有特定状态代码和可选主体的 HTTP 响应。
ResponseEntity<?>即使在像下面这样的官方 Spring 教程中也存在表示法:Building REST services with Spring
使用ResponseEntity<?>而不是 simple的原因是什么ResponseEntity?
这与我之前的问题有点相关:SonarQube 抱怨使用带有通配符的 ResponseEntity
在什么是原始类型中有一些解释,为什么我们不应该使用它?线程,但我想更多地关注 ResponseEntity 类,它在 Spring 控制器中的使用。
考虑下面的代码片段,其中somethingService返回Optional的Something类:
@GetMapping
ResponseEntity<?> getSomething() {
return somethingService.getSomething()
.map(smth -> new ResponseEntity<>(smth, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
Run Code Online (Sandbox Code Playgroud)
是否有任何理由要离开一个通配符,即使我没有因为得到编译器检查任何利润ResponseEntity(HttpStatus status)parametrisesResponseEntity有Object课吗?
| 归档时间: |
|
| 查看次数: |
4442 次 |
| 最近记录: |