Spring boot? The checksum processing specification for spring MVC request parameters

Source: Internet
Author: User
Tags json openid static class
I. Parameter checking 1. Scenarios where parameter verification is required (1) The method of low frequency is called. (2) The method of executing the time overhead is very high. (3) methods to improve stability and usability. (4) Open interface provided externally, API, RPC, HTTP and so on. (5) Access to sensitive authority. 2. Scenarios where calibration parameters are not required (1) a method that is most likely to be called by a loop. (2) The method of the lower level call frequency is relatively high, usually has the error early to expose. (3) is declared as a private method. To ensure that incoming parameters are checked in advance. Two, the parameter passed in

HTTP is a text protocol, and all uploaded parameters and received information are strings. 1.parameters, the parameter is passed to the key value

Example of how 1:parameters

function Test_ajax () {
    $.ajax ({
        type: "POST",
        URL: "TST",
        data: {
            OpenID: "Testajax", sign
            : " Testsign "
        },
        success:function (data) { 
            console.log (data)
        },
        error:function () {
            alert (" Fail!!!, please refresh and try again!);
        }
    );}
function Test_post () {
    $.post ("TST", {"OpenID": "TestOpenid2", "sign": "TestSign2"},function (data) {
        Console.log ("post" +data);}
    );

Equivalent to access http://localhost:8060/tst?openid=testajax&sign=testsign
Note that the parameters are named.

Back end receive HttpServletRequest:

@RequestMapping ("/tst") public
    Object Get_value (httpservletrequest req) {
        map<string, string> Map = Httpheadutil.getparamsmaplimit (req);
        return map;
    }

Jemeter Request data:

Post HTTP://LOCALHOST:8060/TST

post data:
openid=testopneid&sign=testsign

[no cookies]

Request Headers:
connection:keep-alive
content-type:application/x-www-form-urlencoded
content-length:31
host:localhost:8060
user-agent:apache-httpclient/4.5.3 (java/1.8.0_131)

JMeter response Data

{"OpenID": "Testopneid", "sign": "Testsign"}

There are other ways to receive it:

@RequestMapping ("/del_goods")
    void Del_goods (HttpServletRequest req, HttpServletResponse resp, string sn, string SHOP_SN) {}
    void Add_goods (@RequestParam string shop_sn, 
@RequestParam string goods_desc, 
@RequestParam optional< Integer> Point_need,) {
2. Requestbody the way to pass in

The message parameter has no name and is passed in a JSON string, which means that the message body itself is a unique parameter.

function Testbody_ajax () {
    $.ajax ({
        type: "POST",
        dataType: ' json ', 
        contentType: "Application/json" ,
        URL: "Tstbody",
        data: ' {"OpenID": "Testajax", "sign": "Testsign"} ',
        success:function (data) { 
            Console.log (data)
        ,
        error:function () {
            alert ("Fail!!!, please refresh retry!testbody_ajax");}
    );
}

Background receive

    public static class Loginrequestvo implements serializable{
        private static final long serialversionuid = 1L;
        Public String OpenID;
        public String sign;

    @RequestMapping ("/tstbody") public
    Object tstbody (@RequestBody loginrequestvo test) {...}

It is important to note that the message header contenttype: "Application/json", this message header
The message body that represents the upload is a JSON, no parameter name, and the message body itself is a JSON.
This way, spring will do the processing when it receives the JSON and convert it to an object. This is not the time to use Request.getparameters to obtain this request information.
See requests and responses from Jemeter:

Post Http://localhost:8060/tstbody

Post data:
{' OpenID ': ' JMeter ', ' sign ': ' Sign '}

[no cookies]

Request Headers:
connection:keep-alive
content-type:application/json
: 
content-length:33
host:localhost:8060
user-agent:apache-httpclient/4.5.3 (java/1.8.0_131)
{"Result": 1, "data": {"OpenID": "JMeter", "sign": "Sign"}}
other

Note that the previous example of Ajax DataType: ' JSON ', which represents a JSON object instead of a JSON string after receiving the returned result, does not confuse the parameters and headers of spring MVC.

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.