Welcome to my GitHub

Here classification and summary of xinchen all original (including supporting source code) : github.com/zq2599/blog…

This paper gives an overview of

  • Let’s read the source code and learn how Spring Cloud Gateway handles global exception information. After learning so many theories, we can’t help itching to verify the learning effect. Today we’ll write code. The final goal is to rewrite the two red boxes below:

  • For the sake of simplicity, this article will not add a new maven subproject, but will continue to write code in the subproject gateway-change-body.

Download the source code

  • The full source code for this article can be downloaded at GitHub with the following address and link information (github.com/zq2599/blog…
The name of the link note
Project home page Github.com/zq2599/blog… The project’s home page on GitHub
Git repository address (HTTPS) Github.com/zq2599/blog… The project source warehouse address, HTTPS protocol
Git repository address (SSH) [email protected]:zq2599/blog_demos.git The project source warehouse address, SSH protocol
  • The git project has multiple folders. The source code for this project is in the spring-cloud-tutorials folder, as shown in the red box below:

  • There are many sub-projects under the spring-cloud-tutorials folder. This code is gateway-change-body, as shown in the red box below:

ResponseStatusException ()

  • ResponseStatusException = ResponseStatusException = ResponseStatusException = ResponseStatusException = ResponseStatusException = ResponseStatusException Set the return code to 400, message is a Chinese description:

  • Then run the nacOS, provider-Hello project, and gateway-change-body project

  • Try sending a request with Postman. The details of the request and response are shown below:

  • As you can see from the figure above, the return code is 400, as we set it, but message is empty. The request parameter must contain the user-ID field

Little block

  • In our code, we set the message content as shown in the red box below when we throw the exception, but return an empty string when we run it.

  • To see DefaultErrorWebExceptionHandler isIncludeMessage method, see below the errorProperties in the red box, you will suddenly realize: isn’t that what springboot configuration of erro configuration!

  • Modify the project configuration file. The red box shows the new configuration:

  • Try postman again. This time, status, Message, Exception, and Trace are all in one place, exactly as expected:

  • Return ResponseStatusException ();

Second: custom exception with ResponseStatus annotation

  • Next, try the second method: pass the ResponseStatus annotation

  • Mygatewayexception.java (ResponseStatus); ResponseStatus (ResponseStatus);

package com.bolingcavalry.changebody.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@responseStatus (code = httpstatus.forbidden, reason = "User-id cannot be empty ")
public class MyGatewayException extends Exception {}Copy the code
  • After encoding, restart the application and send the request again, as shown in the picture below. The return code and message content are as expected:

  • At this point, both of the simplest ways are validated, and in most cases the requirement is met: pass the error message exactly to the caller

Have flaws

  • You should be smart enough to see the flaws in both of these alternatives: The format and fields that return the body are fixed. What if your project has a strict requirement that only code, message, and data fields are returned, and nothing else is returned?

  • It seems that we need a method, so that we can set the body content as we like, the length is limited, this ultimate solution will be left in the next article, please look forward to, Xinchen original, must live up to you…

You are not alone, Xinchen original accompany all the way

  1. Java series
  2. Spring series
  3. The Docker series
  4. Kubernetes series
  5. Database + middleware series
  6. The conversation series

Welcome to pay attention to the public number: programmer Xin Chen

Wechat search “programmer Xin Chen”, I am Xin Chen, looking forward to enjoying the Java world with you…

Github.com/zq2599/blog…