Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java注释中使用泛型T类型?_Java_Spring Boot_Generics_Inheritance_Annotations - Fatal编程技术网

如何在java注释中使用泛型T类型?

如何在java注释中使用泛型T类型?,java,spring-boot,generics,inheritance,annotations,Java,Spring Boot,Generics,Inheritance,Annotations,我正在为前端开发API,但许多API是类似的,例如: GET foos GET foos/{id} POST foos PUT foos DELETE foos/{id} GET bars GET bars/{id} POST bars PUT bars DELETE bars/{id} 我想在BaseController中加入任何通用逻辑,以减少开发工作,例如: public abstract class BaseController<V> { @GetMapping

我正在为前端开发API,但许多API是类似的,例如:

GET foos
GET foos/{id}
POST foos
PUT foos
DELETE foos/{id}

GET bars
GET bars/{id}
POST bars
PUT bars
DELETE bars/{id}

我想在
BaseController
中加入任何通用逻辑,以减少开发工作,例如:

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);
    }
}
但它不编译

我也试过下面的方法,还是没能编译

protected abstract Class<V> getClazz();

@ApiOperation(value = "Get detail info", response = getClazz())
受保护的抽象类getClazz();
@ApiOperation(value=“获取详细信息”,response=getClazz())

因此,任何其他方式都可以解决此问题?

注释值必须是常量,因此响应值不能是泛型的,因为实际值在编译时未知,也不是常量。也许有一些选择可以绕过这个限制,比如但不是让事情变得更简单

这不是一个完美的解决方案,但您可以做的最佳选择可能是将逻辑与基本控制器EP分离,并让继承类填充注释。在你的基类中:

public abstract class BaseController<V> {
    protected ApiResult detail(String id) {
        V v = getService().getById(id);
        return ApiResult.success(v);
    }
}
公共抽象类BaseController{
受保护的ApiResult详细信息(字符串id){
V=getService().getById(id);
返回apisresult.success(v);
}
}
在继承控制器中:

public class FooController<FooVO> {
    @GetMapping("/{id}")
    @ApiOperation(value = "Get detail info", response = FooVO.class)
    @Override
    protected ApiResult detail(@PathVariable String id) {
        return super(id);
    }
}
公共类控制器{
@GetMapping(“/{id}”)
@ApiOperation(value=“获取详细信息”,response=FooVO.class)
@凌驾
受保护的ApiResult详细信息(@PathVariable字符串id){
返回super(id);
}
}

注意:这个例子当然不好,因为这个方法实际上不需要泛型或基类。服务可以直接注入和调用,而无需调用super。当然,如果您需要在方法的控制器级别处理泛型的其他一些事情,这是下一个最好的选择。

如果您真正关心的是具有类似的API端点,您可以尝试Spring Data Rest API。 这解决了我们在控制器中所做的重复工作

可参考以下链接:

public abstract class BaseController<V> {
    protected ApiResult detail(String id) {
        V v = getService().getById(id);
        return ApiResult.success(v);
    }
}
public class FooController<FooVO> {
    @GetMapping("/{id}")
    @ApiOperation(value = "Get detail info", response = FooVO.class)
    @Override
    protected ApiResult detail(@PathVariable String id) {
        return super(id);
    }
}