Micronaut POST 未创建 DTO class

Micronaut POST not creating DTO class

按照 Micronaut 数据库访问工具包的指南,我有以下 classes 来创建一个新的上传器实体。

上传控制器

@Post
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public UploaderCreatedResponse createUploader(@Body CreateUploaderRequest request) throws UploaderNotFoundException {

    System.out.println("request");
    System.out.println(request);
    System.out.println(request.getUploader());

    return new UploaderCreatedResponse(true, uploaderService.createNewUploader(request.getUploader()));

}

创建上传器请求

package com.digithurst.adminui.controller.requests;

import com.digithurst.adminui.controller.requests.dto.UploaderCreateDTO;
import java.io.Serializable;

public class CreateUploaderRequest implements Serializable {
    private UploaderCreateDTO uploader;

    public CreateUploaderRequest() {
    }

    public CreateUploaderRequest(UploaderCreateDTO uploader) {
        this.uploader = uploader;
    }
    public UploaderCreateDTO getUploader() {
        return uploader;
    }
    public void setUploader(UploaderCreateDTO uploader) {
        this.uploader = uploader;
    }
}

UploaderCreateDTO

package com.digithurst.adminui.controller.requests.dto;

public class UploaderCreateDTO {

    private String api_key;
    private String name;
    private String description;
    private int retention;

    public UploaderCreateDTO() {
    }

    public UploaderCreateDTO(String api_key, String name, String description, int retention) {
        this.api_key = api_key;
        this.name = name;
        this.description = description;
        this.retention = retention;
    }

    public String getApi_key() {
        return api_key;
    }

    public void setApi_key(String api_key) {
        this.api_key = api_key;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getRetention() {
        return retention;
    }

    public void setRetention(int retention) {
        this.retention = retention;
    }
}

当我使用所有参数向服务器发出 POST 请求时,DTO class 未启动。我错过了什么吗?

输出:

com.something.adminui.controller.requests.CreateUploaderRequest@6f485905

您使用 CreateUploaderRequest 作为 @Body 方法参数,而不是 UploaderCreateDTO

所以你需要在你的 JSON 中使用来自 CreateUploaderRequest 的属性,否则反序列化器将无法识别这些字段,而只需使用默认构造函数。

在这种情况下:

{ 
   "uploader" : {
     "api_key": "something",
     ...
   }
}