Home> Java> javaTutorial> body text

Solution to the problem that Spring MVC cannot obtain the value of parameters normally (with code)

不言
Release: 2019-04-13 09:56:07
forward
4387 people have browsed it

The content of this article is about the solution to the problem that Spring MVC cannot obtain the parameter value normally (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

I recently encountered a very strange problem during development. When using the Spring MVC framework in tomcat8, the method parameters in the Controller could not obtain the corresponding values normally. It was solved by changing the tomcat version to 7.0.

Record the following solution process, first the above picture:

##The project adopts ssm architecture, The value of the parameter password cannot be obtained here. I searched for relevant information on the Internet and found the following solutions, but none of them had any effect:

1. Use @RequestParam annotation

for the password parameter. After adding the RequestParam annotation, debugging still cannot obtain the value of password. I change the value of required to true and an error is reported directly. The error means that the password parameter is missing, but the parameter is clearly passed. Why is it still said that the parameter is missing? , could it be that the parameters I passed were not recognized? At this time, it is considered that there may be a problem with the method of passing parameters. Changed the method of passing parameters, appending the value of password to the end of the url, passing parameters just like a GET request. Sure enough, for this reason, the value of the parameter can be received. However, using this parameter passing method, the value of the parameter can be received without adding the RequestParam annotation. Although this method can receive the value of the received parameter, I can't stand the parameter passing method of POST, so I then look for the next method

2. Use @ RequestBody annotation

RequestBody literally means the body of the request. When I made the request, I did put the parameters in the body and passed them over. When I saw this annotation, it seemed like As expected, I couldn't wait to restart tomcat, and then called it with Postman. This time, the breakpoint was not entered, and an exception was reported directly:

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public boolean com.scdq.manager.controller.SystemController.login(java.lang.String)

The general meaning of the exception is that the necessary request body is missing. By querying the relevant information, we found that the RequestBody cannot be like this To use, you need to create a class, define relevant parameters in the class, and then use this class as the type of parameter for the RequestBody annotation

Modified in this way code, restart tomcat again, use Postman to call, and then report an exception again:

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported

The general meaning of the exception is that the content type is application/x-www-form-urlencoded. Generally, the data submitted through the form defaults to this type. I guess it may be the reason for the RequestBody annotation, so I changed it. Parameter passing method: Use JSON format to pass data

Using this parameter passing method, the value of the parameter can indeed be received, but the method of receiving parameters and the method of passing parameters have become complicated. I copied this project home from the company and continued to write it. When I was at the company, everything was debugging fine, but when I returned home, it didn’t work. I was puzzled. I had not changed the same code, so why did something go wrong when I got home? ? Since the codes are the same but the running results are different, it only means that the running environment has changed. So where exactly is the environment different? When this kind of problem occurs, first consider the structure of the database. However, the database version of the home computer is the same as the company computer, and the data is copied directly from the company, so the database is directly ruled out, not to mention that it is received in the Controller. Without the parameters and the step of accessing the database, it is even less likely to be related to the database, so the cause of the database can be ruled out directly. Since the project is built by maven, the relevant jar packages are automatically downloaded by maven, so I wonder if there is a problem with the jar packages that maven automatically downloads. Then delete all the jar packages in the local warehouse and let maven start again. I downloaded it and repeated it several times, but the problem still existed. I had no choice but to temporarily rule out the cause of maven. In the end, I couldn't think of any other reasons, so I began to doubt the cause of tomcat. The company's tomcat is version 7.0, and the tomcat at home is version 8.0. Although the two tomcat versions are inconsistent, I didn't subconsciously think that it must be the tomcat version. After all, I subconsciously think that 8.0 must be an upgrade of functions after 7.0. Although I thought this way, I still went to the test with the mentality of being a dead horse and a live doctor. I downloaded the 7.0 version of the compressed package from the tomcat official website, then configured the corresponding environment and called it with postman. The result was so unexpected. Did you get the parameter value? ! ! ! Is it really because of the tomcat version? This feels too boring. . . . It seems that blindly pursuing higher versions is not a good thing [Related recommendations:Java Tutorial]

The above is the detailed content of Solution to the problem that Spring MVC cannot obtain the value of parameters normally (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!