简体   繁体   English

Spring 可以在通用 controller 中设置 @RequestBody 的值吗?

[英]Can Spring set the values of a @RequestBody in a generic controller?

I have a generic controller with a create() method mapping POST requests using some generic classes.我有一个通用的 controller 和一个使用一些通用类映射 POST 请求的 create() 方法。 I have created a subclass of that controller which provides the specific service and DTO types.我创建了 controller 的子类,它提供特定的服务和 DTO 类型。

In theory, the subclass controller should not need to implement its own create() method.理论上,子类 controller 应该不需要实现自己的 create() 方法。 I have found, however, that if I don't implement a create() method in the subclass, the @RequestBody parameter is not getting properly set when I make requests.但是,我发现,如果我没有在子类中实现 create() 方法,那么当我发出请求时,@RequestBody 参数就没有得到正确设置。

Superclass超类

public abstract class GenericController<InputDtoClass, ModelClass> {
    @PostMapping
    public @ResponseBody ModelClass create(@Valid @RequestBody InputDtoClass input) {
        return service.create(input);
    }
}

Subclass小类

@RestController
@RequestMapping("/my/api/path")
public class SpecificController extends GenericController<SpecificInputDto, SpecificModel> {

}

What I expect is that when I call POST /my/api/path with some data, then my program should call the create() method of the superclass, populate a SpecificInputDto object, then validate it.我期望的是,当我用一些数据调用 POST /my/api/path 时,我的程序应该调用超类的 create() 方法,将 SpecificInputD 填充为 object,然后验证它。

The program is correctly calling the create() method of the superclass, and it's correctly trying to validate the input object according to the annotated fields within the SpecificInputDto class. But the input object itself is not getting any of those values set.该程序正确调用了超类的 create() 方法,并且根据 SpecificInputDto class 中的注释字段正确地尝试验证输入 object。但是输入 object 本身没有设置任何这些值。 (To clarify, if I remove the @Valid tag, I can see that an object of the correct type gets passed, with all fields null). (为了澄清,如果我删除 @Valid 标记,我可以看到传递了正确类型的 object,所有字段均为空)。 This is especially weird given that the program seems to handle generically-typed query parameter classes just fine.考虑到该程序似乎可以很好地处理一般类型的查询参数类,这尤其奇怪。 It's just the field annotated with @RequestBody that's not getting populated, even though Spring appears to know what specific type it's supposed to be.只是用 @RequestBody 注释的字段没有被填充,即使 Spring似乎知道它应该是什么特定类型。

EDIT : If I change the generic controller to use the specific InputDto type, it also fails to populate.编辑:如果我将通用 controller 更改为使用特定的 InputDto 类型,它也无法填充。 But the same exact method, copied into the subclass, works correctly.但是复制到子类中的完全相同的方法可以正常工作。

Apparently, every time I've used VSCode's autocompletion to import @RequestBody , it has decided to import io.swagger.v3.oas.annotations.parameters.RequestBody instead of org.springframework.web.bind.annotation.RequestBody .显然,每次我使用 VSCode 的自动完成功能导入@RequestBody时,它都会决定导入io.swagger.v3.oas.annotations.parameters.RequestBody而不是org.springframework.web.bind.annotation.RequestBody

Importing the correct RequestBody class fixed the issue for me.导入正确的RequestBody class 为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM