An issue encountered with annotation @requestbody in the Spring boot controller

Source: Internet
Author: User
Tags apache tomcat

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.