In Spring MVC 5.3.3, I have a functional pipeline that ends with Optional:

service.doStuff() // returns Optional<Entity>
  .map(Dto::fromEntity)
  .map(ResponseEntity::ok)
  .orElseGet(() -> ResponseEntity.notFound().build());

In this case, I should be able to declare my controller method as returning ResponseEntity<Dto>, but the noContent and notFound builder methods are parameterized with <?>. As they inherently have no bodies, there should not be a conflict with allowing them to pass through the degenerate type parameter <T>, which would permit a clean expression type for "DTO-or-else" expressions.

Comment From: snicoll

The above works for me and it isn't obvious why it doesn't for you looking at the git blame. The ? type you're talking about is for the builder, not the data type. If you can reproduce the problem you've described, please share a small sample (that therefore does not compile for that reason) and we can reconsider.