Location>code7788 >text

BONUS: Additions to Rest style request handling (1)

Popularity:528 ℃/2024-09-10 14:42:08

BONUS: Additions to Rest style request handling (1)

Rest style requests: notes and details

  1. Client is PostMan, you can directly send Put, delete, etc. request, do not set the Filter.

  2. If yo ah SpringBoot supports Rest functionality for page expressions, the following details need to be noted:

  1. Rest style request core Filter: HiddenHttpMethodFilter, the form request will be intercepted by HiddenHttpMethodFilter, get the value of form_method, and then determine the PUT/DELETE/PATCH (the patch method is newly introduced, it's a complement to the Put method. Used for localized updates to known resources :)/q/1010000005685904
  2. If you want SpringBoot to support Rest for page forms, you need to enable the filter function in Otherwise, it will not work.
  3. Modify (under the resources class path) to enable the filter function.

在这里插入图片描述

spring.
  mvc.
    hiddenmethod.
      HiddenHttpMethodFilter.
        enabled: true # Enable rest for page forms, HiddenHttpMethodFilter is enabled, rest is supported.

Rest's core filter:

  1. Current browsers only support post/get requests, so in order to get put/delete requests you need the provided HiddenHttpMethodFilter to convert them.

  2. HiddenHttpMethodFilter : Browser form forms only support get and post requests, but delete, put and other methods are not supported.
    spring adds a filter that converts these requests to standard http parties to support get, post, put and delete requests.

  3. HiddenHttpMethodFilter is able to convert the post request method, so we need to pay special attention to this point

  4. This filter needs to be configured in the

Spring Boot yaml syntax for opening a view parser

spring.
  mvc.
    hiddenmethod.
      HiddenHttpMethodFilter.
        enabled: true # Enable rest for page forms, HiddenHttpMethodFilter is enabled, rest is supported.
    view: # Configure the view parser
      prefix: /rainbowsea/** # Here is something to note, if you configure static-path-pattern: /rainbowsea/** you need to keep it consistent
# prefix: /rainbowsea/ all lines # Note that if you configure static-path-pattern: /rainbowsea/** you need to be consistent.
      suffix: .html
    static-path-pattern: /rainbowsea/**

Let's ponder a question here:Why return "hello" here? It's not a string that's returned, it's forwarded to the corresponding resource file.

在这里插入图片描述

package ;


import ;
import ;

@RestController
public class HiController {


    @RequestMapping("/hello")
    public String hi(){
        return "hi:):)";
    }


    @RequestMapping("/go")
    public String go(){
        return "hello";
        /*

        return is to see if there is a page in the view parser, and if not, look for the controller to handle the request.
if there is no page in the view parser, then look for the controller control, if there is no page in the view parser, then look for a 404 error.
         /* return
    }

}

在这里插入图片描述

Note: I did configure the view parser.
在这里插入图片描述

Start Spring Boot and open a browser to enter:http://localhost:8080/go

在这里插入图片描述

在这里插入图片描述

When a static resource exists, it does not go controller

We will static file resource Remove and revisit:http://localhost:8080/go

Question:

We will static file resource Remove and revisit:http://localhost:8080/go

在这里插入图片描述

在这里插入图片描述

Finally:

"In this final chapter, I would like to express my gratitude to each and every one of my readers. Your attention and responses have been a source of motivation for me to create, and I have drawn endless inspiration and courage from you. I will keep your encouragement in my heart and continue to struggle in other areas. Thanks to you all, we will always meet again at some point."

在这里插入图片描述