There was an unexpected error (type=Not Found, status=404).

╰半橙微兮° 2021-09-23 09:56 615阅读 0赞

使用spring项目访问后台接口时返回了这个错误。

  1. Whitelabel Error Page
  2. This application has no explicit mapping for /error, so you are seeing this as a fallback.
  3. Fri Oct 25 17:31:57 CST 2019
  4. There was an unexpected error (type=Not Found, status=404).
  5. No message available

后台是这样

  1. @Controller
  2. @EnableAutoConfiguration
  3. @RequestMapping("/test")
  4. public class TestController {
  5. @RequestMapping("/testMethod")
  6. public JSON testMethod() {
  7. System.out.println("12345");
  8. Map res = new HashMap();
  9. res.put("data", 321);
  10. return JSONObject.fromObject(res);
  11. }
  12. }

通过后台查看发现,请求确实收到了,但是依然报404 。最后发现是没有设置@ResponseBody。

解决方法,方法上面加上注解@ResponseBody,或者将类的注解@Controller改为@RestController。

@RestController = @Controller + @ResponseBody

修改后的代码

  1. @Controller
  2. @EnableAutoConfiguration
  3. @RequestMapping("/test")
  4. public class TestController {
  5. @RequestMapping("/testMethod")
  6. @ResponseBody
  7. public JSON testMethod() {
  8. System.out.println("12345");
  9. Map res = new HashMap();
  10. res.put("data", 321);
  11. return JSONObject.fromObject(res);
  12. }
  13. }

或者

  1. @RestController
  2. @EnableAutoConfiguration
  3. @RequestMapping("/test")
  4. public class TestController {
  5. @RequestMapping("/testMethod")
  6. public JSON testMethod() {
  7. System.out.println("12345");
  8. Map res = new HashMap();
  9. res.put("data", 321);
  10. return JSONObject.fromObject(res);
  11. }
  12. }

发表评论

表情:
评论列表 (有 0 条评论,615人围观)

还没有评论,来说两句吧...

相关阅读