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

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!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),