How to display default value 0 on swagger-ui
Hi all, My result on swagger is: { "name": "string", "wareHouseID": "Integer", "company": "string", "fullname": "string", "address": "string", "parentID": "Integer" }
but I need "wareHouseID": 0, "parentID": 0. My question: How to set default values 0. My code: private int wareHouseID; private int parentID; private String name; private String fullname; private String company; private String address;
public Warehouse() {
}
@ApiModelProperty(position = 1,required = true)
public int getWareHouseID() {
return wareHouseID;
}
public void setWareHouseID(int wareHouseID) {
this.wareHouseID = wareHouseID;
}
@ApiModelProperty(position = 2,required = true)
public int getParentID() {
return parentID;
}
Thanks all.
May I know which generator are you using? Java? springmvc?
If I understand correctly, you want to set default value (e.g. 0) to model property?
Hi @wing328 i using java and jaxrs
Do you mean you generate the swagger.json from annotation in your code (Java, JAX-RS)?
If you already have a swagger.json to describe your REST API, do the properties have the default attribute? (ref: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#fixed-fields-7)
Can you give me an example :)
Here you go:
"Tag": {
"properties": {
"id": {
"type": "integer",
"format": "int64",
"default": 0
},
"name": {
"type": "string",
"default": "unknown"
}
}
}
Oh, your mean is i have to define swagger.json in my project?
How's your swagger.json generated? using annotation in the code?
My code: @POST @Path(Constants.NEW) @ApiOperation(value = "Create a new warehouse", notes = "Creates a new warehouse", response = Warehouse.class) @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid Id supplied", response = Warehouse.class), @ApiResponse(code = 404, message = "Warehouse not found", response = Warehouse.class) }) @ApiImplicitParams({ @ApiImplicitParam(name = "warehouse", required = true, dataType = "Warehouse", paramType = "body", allowableValues = MediaType.APPLICATION_JSON) }) public @ResponseBody Response createNewWarehouse( @ApiParam(name = "warehouse", required = true) @RequestParam(required = true) Warehouse wh) throws NotFoundException { if(warehouseService.createWarehouse(wh).equals(Constants.SUCCESS)){ String[] errorMessage = new String[]{}; String[] message = new String[]{Constants.SUCCESS}; Object[] rows = new Object[]{wh}; return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.SUCCESS, errorMessage, rows, message)).build(); }else if(warehouseService.createWarehouse(wh).equals(Constants.DUPPLICATE)){ String[] errorMessage = new String[]{Constants.DUPPLICATE}; String[] message = new String[]{}; Object[] rows = new Object[]{}; return Response.status(ApiResponseMessage.DUPPLICATE).entity(new ApiResponseMessage(ApiResponseMessage.DUPPLICATE,errorMessage,rows,message)).build(); }else{ String[] errorMessage = new String[]{Constants.FAIL}; String[] message = new String[]{}; Object[] rows = new Object[]{}; return Response.status(ApiResponseMessage.NO_CONTENT).entity(new ApiResponseMessage(ApiResponseMessage.NO_CONTENT,errorMessage,rows,message)).build(); } }
I don't have swagger.json :(
i don't see swagger.json here: https://github.com/swagger-api/swagger-codegen/tree/master/samples/server/petstore/jaxrs