Search code examples
javadtomicronautmicronaut-data

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.

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

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. Am I missing something?

enter image description here

Output:

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

null


Solution

  • You're using the CreateUploaderRequest for the @Body method parameter, not the 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.

    In this case:

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