Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the request body in Swagger 1.5 to an example JSON request body

Current look of Swagger request

Is it possible to change the body

{
  "config": {
    "additionalProp1": {},
    "additionalProp2": {},
    "additionalProp3": {}
  },
  "class": "string"
}

to look like

{
"class": "my.class.com",
"config": {
    "myParam": "",
    "datacenter": "USA",
}

Currently the method looks like this

@POST
@Path("/run")
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Runs a specified job asynchronously",
    notes = "Doesn't work for full tasks.")
public Response createTask(TaskConfig taskConfig) {

TaskConfig is a basic class with two member variables

public class TaskConfig {
    @JsonProperty("class")
    @NotNull
    private String clazz;

    private Map<String, Object> config;

We are using Swagger 1.5 from this Dropwizard Swagger bundle library. I know that 2.0 has the @RequestBody annotation and I just want to make sure that is my only option before I go down the path of upgrading.

like image 507
Ryan Mavilia Avatar asked Jan 26 '26 00:01

Ryan Mavilia


1 Answers

Even by using @RequestBody we need to add additional annotation within TaskConfig class.

We are using Swagger 2.0.x. The TaskConfig class would have fields something like this :

@Schema(
  description = " My descriptions",
  type = "array",
  example = " {\"myParam\" :\"value\" ,"
      + "\"datacenter\": \"USA\"}")
private Map<String, Object> config;

Similarly clazz can also be annotated like :

@Schema(description = "The field descrition", example = "true")

And RequestBody annotation will look like :

@RequestBody(
  description = "Description of TaskConfig ",
  content = @Content(schema = @Schema(implementation = TaskConfig .class)))

I got the result as shown in the image:

enter image description here

like image 73
user2122524 Avatar answered Jan 27 '26 16:01

user2122524



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!