How to add error response format in Swagger for a Spring-boot project based on webflux

A. Henteti

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.

Misantorp

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);
}

Swagger result

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Spring Boot + Springbox swagger error

分類Dev

Capturing response in spring Webflux

分類Dev

How to add a dependency to a Spring Boot Jar in another project?

分類Dev

How to add a dependency to a Spring Boot Jar in another project?

分類Dev

Suddenly Springfox Swagger 3.0 is not working with spring webflux

分類Dev

Spring Boot Actuator / Swagger

分類Dev

Not able to add JPA dependency into spring-boot project

分類Dev

How to add src/dist to spring boot distribution

分類Dev

How to reference to a response object with swagger?

分類Dev

Spring webflux error handler: How to get the reactor context of the request in the error handler?

分類Dev

Support multiple pathmapping in Swagger UI/Spring boot

分類Dev

How to format the HTTP response

分類Dev

How to edit Hibernate settings in a Spring-Boot project?

分類Dev

How to edit Hibernate settings in a Spring-Boot project?

分類Dev

How to edit Hibernate settings in a Spring-Boot project?

分類Dev

Springboot WebFlux tests failing after upgrade to spring boot 2.2

分類Dev

Spring Boot WebfluxリアクティブAPI

分類Dev

How to setup a java 11 based spring boot pipeline on bitbucket?

分類Dev

Webflux, How to intercept a request and add a new header

分類Dev

Multi module project with spring boot

分類Dev

Multi module project with spring boot

分類Dev

How to set a timeout in Spring 5 WebFlux WebClient

分類Dev

How to set a timeout in Spring 5 WebFlux WebClient

分類Dev

How to use WebSession in Spring WebFlux to persist data?

分類Dev

How to set response model as a class in Springfox Swagger?

分類Dev

Spring Boot: How to add another WAR files to the embedded tomcat?

分類Dev

How to add SAML token to CXF client request in Spring Boot

分類Dev

Spring Boot + Spring Boot Security Start Error

分類Dev

swagger-uiおよびSpring webfluxでの404エラー

Related 関連記事

  1. 1

    Spring Boot + Springbox swagger error

  2. 2

    Capturing response in spring Webflux

  3. 3

    How to add a dependency to a Spring Boot Jar in another project?

  4. 4

    How to add a dependency to a Spring Boot Jar in another project?

  5. 5

    Suddenly Springfox Swagger 3.0 is not working with spring webflux

  6. 6

    Spring Boot Actuator / Swagger

  7. 7

    Not able to add JPA dependency into spring-boot project

  8. 8

    How to add src/dist to spring boot distribution

  9. 9

    How to reference to a response object with swagger?

  10. 10

    Spring webflux error handler: How to get the reactor context of the request in the error handler?

  11. 11

    Support multiple pathmapping in Swagger UI/Spring boot

  12. 12

    How to format the HTTP response

  13. 13

    How to edit Hibernate settings in a Spring-Boot project?

  14. 14

    How to edit Hibernate settings in a Spring-Boot project?

  15. 15

    How to edit Hibernate settings in a Spring-Boot project?

  16. 16

    Springboot WebFlux tests failing after upgrade to spring boot 2.2

  17. 17

    Spring Boot WebfluxリアクティブAPI

  18. 18

    How to setup a java 11 based spring boot pipeline on bitbucket?

  19. 19

    Webflux, How to intercept a request and add a new header

  20. 20

    Multi module project with spring boot

  21. 21

    Multi module project with spring boot

  22. 22

    How to set a timeout in Spring 5 WebFlux WebClient

  23. 23

    How to set a timeout in Spring 5 WebFlux WebClient

  24. 24

    How to use WebSession in Spring WebFlux to persist data?

  25. 25

    How to set response model as a class in Springfox Swagger?

  26. 26

    Spring Boot: How to add another WAR files to the embedded tomcat?

  27. 27

    How to add SAML token to CXF client request in Spring Boot

  28. 28

    Spring Boot + Spring Boot Security Start Error

  29. 29

    swagger-uiおよびSpring webfluxでの404エラー

ホットタグ

アーカイブ