Record the problem of calling the http interface successfully but returning 404
The problem manifests itself as follows:
1. The front end calls the http interface, and the interface returns 404
2. Through debug confirmation, the interface call is successful
3. The interface will return JSON data
Here a case is used to demonstrate the problem
1. Backend interface code
@Controller
@RequestMapping("test")
public class TestController {
@RequestMapping("testReturnStr")
public String testReturnStr(HttpServletRequest request, HttpServletResponse response) {
return "盖伦与卡特琳娜";
}
}
2. The code returned by the front-end call is:404
, but in fact the call is normal, so I won’t take a screenshot here
Reason: The back-end interface returns JSON data, but the controller does not add relevant declarations
solve
Method 1: Change the controller @Controller
to@RestController
//使用@RestController
@RestController
@RequestMapping("test")
public class TestController {
@RequestMapping("testReturnStr")
public String testReturnStr(HttpServletRequest request, HttpServletResponse response) {
return "盖伦与卡特琳娜";
}
}
@ResponseBody
Method 2: Add annotations on the controller method
@Controller
@RequestMapping("test")
public class TestController {
//添加@ResponseBody
@ResponseBody
@RequestMapping("testReturnStr")
public String testReturnStr(HttpServletRequest request, HttpServletResponse response) {
return "盖伦与卡特琳娜";
}
}