0

I have the following ApiModel for my endpoint -

public class CreateConfigRequest {
    @ApiModelProperty(example = "hive")
    String entityType;
    @ApiModelProperty(example = "imports")
    String entityNamespace;
    @ApiModelProperty(example = "hotel")
    String entityName;
    @ApiModelProperty(example = "{\"name\": \"hotel\", \"batch\": {\"type\": \"FullScan\"}}")
    JobConfig content;
}

Where JobConfig is another pojo class. Code below :

@Data
public class JobConfig {
    @NonNull private String name;
    @NonNull private BatchSpec batch;
    private ProfileConfig profile;
    private ValidateConfig validate;
    private ActionConfig action;
}

My swagger looks like - enter image description here

Which is basically the structure of the POJO.

How it should look like -

enter image description here

Basically i am looking to understand how i can set it to a default JSON structure.

Shiv
  • 105
  • 7

1 Answers1

1

If you don't want to include profile, validate and action at all in your JSONs you can simply use @JsonIgnore as follows:

@Data
public class JobConfig {
    @NonNull private String name;
    @NonNull private BatchSpec batch;

    @JsonIgnore
    private ProfileConfig profile;

    @JsonIgnore
    private ValidateConfig validate;

    @JsonIgnore
    private ActionConfig action;
}

If you simply don't want to list them in Swagger documentation, you can use @ApiModelProperty as follows:

@Data
public class JobConfig {
    @NonNull private String name;
    @NonNull private BatchSpec batch;

    @ApiModelProperty(hidden = true)
    private ProfileConfig profile;

    @ApiModelProperty(hidden = true)
    private ValidateConfig validate;

    @ApiModelProperty(hidden = true)
    private ActionConfig action;
}

Given that you don't want to hide properties but instead show a more realistic example, try the following:

@Data
public class JobConfig {
    @NonNull 
    @ApiModelProperty(example = "hotel")
    private String name;

    @NonNull 
    private BatchSpec batch;

    private ProfileConfig profile;

    private ValidateConfig validate;

    private ActionConfig action;
}
@Data
public class BatchSpec {
    @ApiModelProperty(example = "FullScan")
    private String type;
}
João Dias
  • 16,277
  • 6
  • 33
  • 45
  • I don't want to hide it completely, Right now it is showing default structure. I want to create an object and show it on swagger. – Shiv Nov 25 '21 at 10:13
  • Sorry, I am not following you. Then what do you want to not show and when? – João Dias Nov 25 '21 at 10:14
  • Right now if you see in the image, it shows "String". I want to put a reasonable value there so create a jobConfig object of my choice and then show it on the swaggger. – Shiv Nov 25 '21 at 10:17
  • Now I understand what you mean. `@ApiModelProperty(example = )` should do it, but I would say you need to annotate each property in `JobConfig` and also in `BatchSpec`, `ProfileConfig`, `ValidateConfig` and `ActionConfig` properties. – João Dias Nov 25 '21 at 10:42
  • Thanks for your reply, If you see I have @ApiModelProperty(example = "{\"name\": \"hotel\", \"batch\": {\"type\": \"FullScan\"}}") but it does not reflect anything. Also this accepts only String, I don't know how to give it a variable here. – Shiv Nov 25 '21 at 11:46
  • That is not exactly what I suggested. I will updated my answer. – João Dias Nov 25 '21 at 11:48
  • How stupid of me to miss this. Thanks for your help! Makes a lot lot more sense. :) – Shiv Nov 25 '21 at 12:27
  • The devil is in the details ;) You are welcome! – João Dias Nov 25 '21 at 12:30