Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report