I am developing API to front end, but many APIs are similar, e.g:
GET foos
GET foos/{id}
POST foos
PUT foos
DELETE foos/{id}
GET bars
GET bars/{id}
POST bars
PUT bars
DELETE bars/{id}
I want to put any common logic in BaseController
to reduce developing work, e.g:
public abstract class BaseController<V> {
@GetMapping("/{id}")
@ApiOperation(value = "Get detail info", response = FooVO.class)
protected ApiResult detail(@PathVariable String id) {
V v = getService().getById(id);
return ApiResult.success(v);
}
}
but I also want to support many concrete Controllers:
public class FooController extends BaseController<FooVO>
public class BarController extends BaseController<BarVO>
...
So response class should be dynamically map to generic V
@ApiOperation(value = "Get detail info", response = FooVO.class)
==>
@ApiOperation(value = "Get detail info", response = V.class)
but it does not compile.
I also tried below way, still failed to compile
protected abstract Class<V> getClazz();
@ApiOperation(value = "Get detail info", response = getClazz())
So any other manner could solve this problem?