A summary of the issues encountered by using annotations @requestbody in the Spring boot controller:
Receive entity objects through @requestbody, as shown in the code
@PostMapping (value = "AddType")
Public Object Addapptype (@RequestBody apptype apptype) throws exception{
return jsondata.buildsuccess ();
}
When using postman to test the interface,
- First select the POST request
- Then add "Content-type:application/json" to the header.
- :
- Select "Raw" in the body and select "JSON (Application/json)"
- :
This allows you to send a POST request to test Spring boot's post interface after this configuration.
Tread on the Pit:
The body in postman selected "x-www-form-urlencoded" to send the request. The error is as follows (this looks like an external tomcat error):
<!DOCTYPE HTML><HTMLLang= "en"><Head><title>HTTP Status 404? Not Found</title><styletype= "Text/css">H1{font-family:Tahoma,arial,sans-serif;Color: White;Background-color:#525D76;font-size:22px;}H2{font-family:Tahoma,arial,sans-serif;Color: White;Background-color:#525D76;font-size:16px;}H3{font-family:Tahoma,arial,sans-serif;Color: White;Background-color:#525D76;font-size:14px;}Body{font-family:Tahoma,arial,sans-serif;Color:Black;Background-color: White;}b{font-family:Tahoma,arial,sans-serif;Color: White;Background-color:#525D76;}P{font-family:Tahoma,arial,sans-serif;background: White;Color:Black;font-size:12px;}a{Color:Black;}A.name{Color:Black;}. Line{Height:1px;Background-color:#525D76;Border:None;}</style></Head><Body><H1>HTTP Status 404? Not Found</H1><HRclass= "line" /><P><b>Type</b>Status Report</P><P><b>Description</b>The origin server did not find a current representation for the target resource or was not willing to disclose that one Exi Sts.</P><HRclass= "line" /><H3>Apache tomcat/9.0.10</H3></Body></HTML>
This error is also reported if the Postman request mode is selected as a put at this time. (It's strange to guess if the external tomcat blocked the actual error?)
In addition, I found that using postman in the body to select the "x-www-form-urlencoded", controller inside can not write @requestbody, directly with the entity class receive the data will be received. The code is as follows:
@PostMapping (value = "AddType") publicthrows exception{return jsondata.buildsuccess (); }
But this approach doesn't seem to work because the spring boot + Vue frontend accesses the interface in JSON format ...
An issue encountered with annotation @requestbody in the Spring boot controller