Handwrite the DispatcherServlet central controller to obtain the name of the parameter parameter.getName(). The obtained name is arg0, arg1, arg2…, resulting in the failure to receive the token passed from the front end. When I logged in, it kept showing that the login had expired. I didn’t know that I had not received the token at that time. I always thought it was a code problem with the central controller. I wrote this blog to record this moment, my growth process, and the problems I solved would make me happy. I understood the process of this process, analyzed the code, debugged countless times, and finally found the problem. This experience also made me understand the importance of debugging, a tool for analyzing code. Okay, let’s start with a summary of how to solve this problem.
1. The first solution
It is configured in the idea settings-parameters, but unfortunately, my problem has not been solved, because I configured it like this for the first time, so when I checked the code, I always thought It has nothing to do with the name of this parameter arg0. I thought it was not because of this at the time. In fact, the problem is still due to the influence of the name of the parameter, but this configuration has no effect on my code, hahahaha.
2. The second solution
Because I summarized this knowledge in the Maven environment, I can configure this -parameters in the pom.xml file and configure build in the pom.xml. After configuring this, my problem was solved. The first one The solution idea has no effect on my code.
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>17</source> <target>17</target> <compilerArgs> <arg>-parameters</arg> </compilerArgs> </configuration> </plugin> </plugins> </build>3. The third solution
If you are using Gradle, you can add the following configuration in the build.gradle file:
compileJava { options.compilerArgs + = "-parameters" }4. The fourth solution
If you are using the command line compiler, you can use the following command:
javac -parameters MyClass.javaPlease note that this option only applies to method parameters, not local variables or field names. If you need to get local variables or field names, you can use a Java bytecode library such as ASM or ByteBuddy
parameter.getName()
The parameter name obtained is the name of the formal parameter. If no formal parameter name is specified, the default isarg0
,arg1
,arg2
, etc. To solve this problem, there are several ways:
- Specify parameter names in interface methods
You can use the
@WebParam(name = "paramName")
annotation to specify the parameter name in the interface method, for example:@WebMethod int add(@WebParam(name = "a") int num1, @WebParam(name = "b") int num2);In this way, when
parameter.getName()
is called, the specified parameter name will be returned.
- Use reflection to get method parameter names
Get the parameter name by using the
Parameter.getName()
method in the reflection API introduced in Java 8. You need to use Java 8 or higher, and you need to enable the-parameters
option when compiling, for example:javac -parameters MyClass.javaThen use reflection to get the parameter name, for example:
Method method = MyClass.class.getMethod("add", int.class, int.class); Parameter[] parameters = method.getParameters(); String paramName = parameters[0].getName(); // Get the name of the first parameter
- Use third-party libraries
You can use some third-party libraries to obtain method parameter names, such as the
org.springframework.core.LocalVariableTableParameterNameDiscoverer
class in Spring Framework, orcom.google.common.reflect in the Google Guava library. Parameter
class. These libraries can obtain method parameter names at runtime and do not require the-parameters
option to be enabled at compile time.
Generally speaking, if the parameters returned by
parameter.getName()
are namedarg0
,arg1
, etc., it is because the compiler does not know the specific parameters of the parameters. names, so they are namedarg0
,arg1
etc. This problem can be solved by using the-parameters
option when compiling. This option tells the compiler to preserve argument names at compile time instead of naming themarg0
,arg1
, etc. For example, usejavac -parameters MyClass.java
, whereMyClass.java
is the source code file that contains your classes and methods.If you are unable to use the
-parameters
option at compile time, you can try using reflection to get the parameter names. Using reflection requires theParameter.getName()
method introduced in Java 8. If your runtime environment is pre-Java 8, you can use third-party libraries such as Javassist or ASM to achieve the same functionality. It is important to note that using reflection or a third-party library to obtain parameter names may cause performance issues and may cause code readability and maintainability issues.
You can try to use the getParameterTypes() method to get the parameter type, and then determine the parameter name based on the type. For example:
Method method = ... // Get method object Parameter[] parameters = method.getParameters(); for (int i = 0; i < parameters.length; i + + ) { Parameter parameter = parameters[i]; String parameterName = parameter.getName(); if (parameter.getType() == int.class) { parameterName = "intValue"; } else if (parameter.getType() == String.class) { parameterName = "stringValue"; } System.out.println("Parameter " + i + " name: " + parameterName); }In this example, we determine the parameter name by determining the parameter type. Of course, you can also use other rules to determine parameter names.
When using the
getParameter
method to obtain the parameter name, if the parameter name is not specified, the default values will be assigned toarg0
,arg1
,arg2
… etc. can be solved in the following two ways:
- Specify parameter name
When defining the interface, you need to specify the name of each parameter, for example:
public interface UserService { void addUser(@RequestParam("name") String name, @RequestParam("age") int age); }In this way, when calling the
getParameter
method, you can get the specific parameter name.
- Using Spring AOP
Using Spring AOP, method-level interception can be achieved. Method parameters can be obtained before the method is called, and parameter names can be obtained through
ParameterNameDiscoverer
, for example:@Component @Aspect public class LogAspect { @Autowired private ParameterNameDiscoverer parameterNameDiscoverer; @Before("execution(* com.example.UserService.addUser(..))") public void beforeAddUser(JoinPoint joinPoint) { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature.getMethod(); String[] parameterNames = parameterNameDiscoverer.getParameterNames(method); Object[] args = joinPoint.getArgs(); for (int i = 0; i < args.length; i + + ) { System.out.println(parameterNames[i] + " = " + args[i]); } } }In this way, when calling the
addUser
method, you can intercept it through AOP and obtain the parameter name before calling the method.
The parameter names are arg0, arg1, etc. because Java uses parameter names to reference parameters by default. If no specific flag is used at compile time, the compiler will set the parameter names to arg0, arg1, etc. One way to solve this problem is to use the -parameters flag when compiling. This will tell the compiler to compile the parameter names into bytecode. For example, when using Maven to build a project, you can add the following plugin configuration in the pom.xml file:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <compilerArgs> <arg>-parameters</arg> </compilerArgs> </configuration> </plugin> </plugins> </build>This forces the compiler to compile the parameter names into bytecode so they can be accessed at runtime.
You may encounter this issue if you are using an older web services framework such as JAX-WS or JAX-RPC. This is due to these frameworks identifying parameters using their position rather than their name.
To solve this problem, you can use a more modern web services framework, such as JAX-RS or Spring Web Services, which support identifying parameters by name instead of position. You can also try using some library such as Apache Axis to enable parameter name support.
If you cannot change the web service framework or use a library, you can manually specify the parameter names by adding annotations on the web service methods. For example, in JAX-WS, you can use the @WebParam annotation to specify parameter names as follows:
public String myWebServiceMethod(@WebParam(name = "myParameter") String myParameter) { // ... }This will make the parameter name “myParameter” instead of the default name “arg0”.
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 139421 people are learning the system