About the problem of reporting 404 when accessing the back-end interface - the most detailed 404 error in the whole network

When we initiate a request to the backend through the frontend to call the backend interface, we often encounter 404 problems. There are a lot of introductions to the 404 problem on the Internet. In fact, the essence of the 404 problem is two points.

Before introducing the 404 problem, let’s review a small knowledge point-project access path
Project access path: It is the path to locate a project, which can be understood as the project name, but generally this name can be customized. Before SpringBoot version 2.0, the server.context-path configuration was used when configuring the project access path in the yml file,
while after SpringBoot version 2.0, the project access path was configured using server.servlet.context-path.

The first point is whether the back-end interface can be accessed.
Here I posted a piece of code, including the application.yml configuration file. All subsequent demonstrations are based on this code and configuration file.

package com.redisson.controller;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/redisson")
public class RedissonController {
    
    

    @Autowired
    RedissonClient redissonClient;

    @RequestMapping("/testRedissonClient")
    public void TestRedisson() {
    
    
        System.out.println("testRedissonClient: "+redissonClient);
    }
}

insert image description here
Let's start calling the interface and let the 404 error appear. (Postman test is used here)
Situation 1: The server.servlet.context-path (project access path) is missing or the name is wrong
insert image description here
insert image description here
Situation 2: The value in the @RequestMapping("/demo") annotation on the class is missing or the name is writing wrong

insert image description here
insert image description here

Case 3: The value in the @RequestMapping("/demo") annotation above the method is missing or the name is wrong
insert image description here

insert image description here
Situation 4: The path has more content
insert image description here

Summary:
The access path behind the port number is wrongly written, resulting in 404 problems when accessing the back-end interface. The direct consequence of this kind of error is that the request cannot enter the backend interface.
.Solution
:
There is a 404 problem, and you must carefully check whether the address of the access interface is correct. The
general request for access is: project access path + value address value configured by @RequestMapping above the class + value address value configured by @RequestMapping above the class

The second point is whether the return value of the back-end interface can be mapped to the front-end page. If the
interface access path is written correctly and the interface can be accessed successfully, the 404 problem will still occur. The specific operation case is as follows.
insert image description here

package com.redisson.controller;

import com.redisson.bean.Person;
import com.redisson.utils.Msg;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/demo")
public class RedissonController {
    
    

    @Autowired
    RedissonClient redissonClient;

//    返回值是void,报404异常
    @RequestMapping("/testRedissonClient")
    public void TestRedisson() {
    
    
        System.out.println("testRedissonClient: "+redissonClient);
    }

//返回值是null,报404异常
    @RequestMapping("/testRedissonClient2_1")
    public Msg TestRedisson2_1() {
    
    
        System.out.println("testRedissonClient2_1: "+redissonClient);
        return null;
    }

//  返回值是空字符(比如 ""),报404异常
    @RequestMapping("/testRedissonClient2_2")
    public String TestRedisson2_2() {
    
    
        System.out.println("testRedissonClient2_2: "+redissonClient);
        return "";

    }

//  返回值是任意字符串(比如 "success"),报404异常
    @RequestMapping("/testRedissonClient2")
    public String TestRedisson2() {
    
    
        System.out.println("testRedissonClient2: "+redissonClient);
        return "success";
    }
//返回值是虽说是null,但是在@ResponseBody注解的加持下,则不会出现404异常问题
    @RequestMapping("/testRedissonClient3")
    @ResponseBody
    public Msg TestRedisson3() {
    
    
        System.out.println("testRedissonClient3: "+redissonClient);
        return null;
    }

    //返回值是虽说是空字符,但是在@ResponseBody注解的加持下,则不会出现404异常问题
    @RequestMapping("/testRedissonClient4")
    @ResponseBody
    public String TestRedisson4() {
    
    
        System.out.println("testRedissonClient4: "+redissonClient);
        return "";
    }
    //返回值是虽说是null,但是在@ResponseBody注解的加持下,则不会出现404异常问题
    @RequestMapping("/testRedissonClient5")
    @ResponseBody
    public String TestRedisson5() {
    
    
        System.out.println("testRedissonClient5: "+redissonClient);
        return null;
    }

    //返回值是是一个json对象,但是在@ResponseBody注解的加持下,则不会出现404异常问题
    @RequestMapping("/testRedissonClient6")
    @ResponseBody
    public Msg TestRedisson6() {
    
    
        System.out.println("testRedissonClient6: "+redissonClient);
        return Msg.success("访问成功!").data("person",new Person());
    }

    //返回值是是一个json对象,但是在没有@ResponseBody注解的加持下,则会出现404异常问题
    @RequestMapping("/testRedissonClient7")
    public Msg TestRedisson7() {
    
    
        System.out.println("testRedissonClient6: "+redissonClient);
        return Msg.success("访问成功!").data("person",new Person());
    }
}

Summary:
1. For methods with @ResponseBody annotation added, after requesting access to the method, 404 exceptions will not be reported when returning to the front-end page. The reason is that the return value of the @ResponseBody annotation modified method is not parsed by the view resolver.
.

2. The access path behind the port number is written correctly, and the request can enter the interface, but a 404 exception is reported when returning to the front-end page. The direct consequence of this kind of error is that the response result cannot be displayed on the page.
.Analysis
:
The reason why this kind of problem occurs is because springBoot defaults without the support of @ResponseBody annotation, the returned result is parsed by the view parser and matches a suitable front-end page, and then the result is displayed . When we did not write the corresponding front-end page in the project, the parsed result of the natural view parser could not be matched, so a 404 exception was reported. . . Solution: A 404 problem
occurred
.
First, check whether the @ResponseBody annotation has been added. Second, Carefully check that we did not write the corresponding front-end page in the project.
(1) Add the @ResponseBody annotation on the method of the controller class or on the controller class
(2) The return value of the method must be able to find the corresponding front-end page in the project.

Guess you like

Origin blog.csdn.net/CNCDXX_88/article/details/129489299