简体   繁体   English

Micronaut POST 不创建 DTO 类

[英]Micronaut POST not creating DTO class

Following the guidelines of the Micronaut database access toolkit, I have the following classes in order to create a new Uploader entity.按照 Micronaut 数据库访问工具包的指导方针,我有以下类来创建一个新的 Uploader 实体。

UploaderController上传控制器

@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()));

}

CreateUploaderRequest创建上传请求

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上传者创建DTO

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;
    }
}

When I issue a POST request to the server with all the parameters the DTO class is not initiated.当我使用所有参数向服务器发出 POST 请求时,不会启动 DTO 类。 Am I missing something?我错过了什么吗?

在此处输入图片说明

Output:输出:

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

null无效的

You're using the CreateUploaderRequest for the @Body method parameter, not the UploaderCreateDTO .您将CreateUploaderRequest用于@Body方法参数,而不是UploaderCreateDTO

So you need to use the properties from CreateUploaderRequest in your JSON, otherwise the deserializer won't recognize the fields, and simply use the default constructor.所以你需要在你的 JSON 中使用CreateUploaderRequest的属性,否则反序列化器将无法识别这些字段,而只需使用默认构造函数。

In this case:在这种情况下:

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

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

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