springboot三层架构解决报错信息:o.a.c.c.C.[.[.[].[dispatcherServlet] : Servlet.service() for servlet
作者:mmseoamin日期:2023-12-21

报错信息:

2023-07-23 21:20:50.175 ERROR 4380 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Circular view path [depts]: would dispatch back to the current handler URL [/depts] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause
javax.servlet.ServletException: Circular view path [depts]: would dispatch back to the current handler URL [/depts] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
	at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:210) ~[spring-webmvc-5.3.29.jar:5.3.29]
	at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:148) ~[spring-webmvc-5.3.29.jar:5.3.29]
	at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:316) ~[spring-webmvc-5.3.29.jar:5.3.29]
	at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1406) ~[spring-webmvc-5.3.29.jar:5.3.29]
	at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1150) ~[spring-webmvc-5.3.29.jar:5.3.29]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1089) ~[spring-webmvc-5.3.29.jar:5.3.29]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:965) ~[spring-webmvc-5.3.29.jar:5.3.29]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.29.jar:5.3.29]
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.29.jar:5.3.29]

postman测试返回的报错信息:

{
    "timestamp": "2023-07-23T13:20:50.177+00:00",
    "status": 500,
    "error": "Internal Server Error",
    "path": "/depts"
}

controller层代码:

@Controller
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping("/depts")
    public Result selectAll(){
        List depts = deptService.selectAll();
        return Result.success(depts);
    }

解决办法是将注释@Controller改成@RestController,就可以成功运行,原因我也不知道,刚学这个没多久,记录下来,如果有人能告诉我·原因的话,我会非常感激的。