Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method for Spring to get request input and output parameters?

Shulou Source: shulou.com Published: 2022-06-03 01:35:47 10月14日 Update

The main content of this article is to explain "what is the method for Spring to obtain input and output parameters". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the method for Spring to get request for input and output parameters"?

Filter

If you get the request information in a non-intrusive situation, you must think of a filter. The filter can get two messages, HttpServletRequest and HttpServletResponse, and you must think that I can get the information I want with these two objects. Then let's try to get the corresponding information.

Custom HttpServletRequest

1. Write a class that inherits ServletInputStream from the class input stream:

two。 Write a custom Request inheritance HttpServletRequestWrapper:

3. The great task has been completed

Custom HttpServletResponse

1. Write a class that inherits ServletOutputStream from the class output stream:

two。 Write a custom Response inheritance HttpServletResponseWrapper:

3. Make a great achievement

AOP mode

Breakpoint tracking source code

1. First write a test class

two。 Break point.

3. You can see from here that the input parameter is obtained from getMethodArgumentValues, so let's take a look at this source code.

4. Method parameters are taken from the MethodParameter object, so let's take a look at this class

5. Here is the parameter name. I'll break it again to see if the parameter name is desired.

6. Yes, this is the desired parameter name, and then this.resolvers.resolveArgument gets the parameter value, so let's see where resolvers is assigned.

Here

Where there's a will, there's a way

See where to call this method.

Go in and have a look

The object processor settings for the input and output parameters are all here. Let's take a look at the origin of this class.

The general explanation is that

Support for custom parameters and return value types can be achieved by adding

* {@ link # setCustomReturnValueHandlers} and {@ link # setCustomReturnValueHandlers}

* alternatively, reconfigure all parameters and return value types

* use {@ link # setArgumentResolvers} and {@ link # setReturnValueHandlers}

Write a custom MethodArgumentResolver

Add output and input parameter processor

Printing effect

At this point, I believe you have a deeper understanding of "what is the method for Spring to obtain input and output parameters". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Parameters methods information objects breakpoints filters two content processor great work that is source code type processing learning affirmation practicality depth great success good work. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia Microsoft MySQL macOS