使用spring項目訪問後臺接口時返回了這個錯誤。
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Oct 25 17:31:57 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available
後臺是這樣
@Controller
@EnableAutoConfiguration
@RequestMapping("/test")
public class TestController {
@RequestMapping("/testMethod")
public JSON testMethod() {
System.out.println("12345");
Map res = new HashMap();
res.put("data", 321);
return JSONObject.fromObject(res);
}
}
通過後臺查看發現,請求確實收到了,但是依然報404 。最後發現是沒有設置@ResponseBody。
解決方法,方法上面加上註解@ResponseBody,或者將類的註解@Controller改爲@RestController。
@RestController = @Controller + @ResponseBody
修改後的代碼
@Controller
@EnableAutoConfiguration
@RequestMapping("/test")
public class TestController {
@RequestMapping("/testMethod")
@ResponseBody
public JSON testMethod() {
System.out.println("12345");
Map res = new HashMap();
res.put("data", 321);
return JSONObject.fromObject(res);
}
}
或者
@RestController
@EnableAutoConfiguration
@RequestMapping("/test")
public class TestController {
@RequestMapping("/testMethod")
public JSON testMethod() {
System.out.println("12345");
Map res = new HashMap();
res.put("data", 321);
return JSONObject.fromObject(res);
}
}