Reputation: 1128
I don't know how to add an error response format in Swagger for a Spring-boot project based on webflux.
In all the tutorials that I have read I have only found how to add 200 response format but I also need to show the format of the error (the map<String, Object>
) generated by spring-boot. Any ideas please.
Upvotes: 2
Views: 7985
Reputation: 2821
As you pointed out yourself, the default error response payload is a Map
(specifically a Map<String, Object>
, see DefaultErrorAttributes.getErrorAttributes(...)). Specifying this as the error response will not get you what you want.
Instead you can specify the error response payload creating a class
with the fields of the default error map.
public class SpringErrorPayload {
public long timestamp;
public int status;
public String error;
public String exception;
public String message;
public String path;
public SpringErrorPayload(long timestamp, int status, String error, String exception, String message, String path) {
this.timestamp = timestamp;
this.status = status;
this.error = error;
this.exception = exception;
this.message = message;
this.path = path;
}
//removed getters and setters
}
Then, by adding a class
to the response
attribute of the @ApiResponse
of your method, you will get the desired result
@ApiOperation("Create new something")
@ApiResponses(value = {
@ApiResponse(code = 201, message = "New something successfully created"),
@ApiResponse(code = 400, message = "Bad request, adjust before retrying", response = SpringErrorPayload.class),
@ApiResponse(code = 500, message = "Internal Server Error", response = SpringErrorPayload.class )
})
@ResponseStatus(CREATED)
public SomethingPayload createSomething(@Valid @RequestBody final NewSomethingPayload newSomethingPayload) {
return somethingService.createSomething(newSomethingPayload);
}
Upvotes: 3