Spring MVC “duplicates” parts of the URL

后端 未结 4 1348
攒了一身酷
攒了一身酷 2021-02-14 12:14

I have a simple @Controller within a spring-mvc environment. This is the Controller:

@Controll         


        
相关标签:
4条回答
  • 2021-02-14 12:24

    Do you have a file named messaging-servlet.xml that declares the web application context? By default because you have named the DispatcherServlet messaging, Spring will try to locate that file.

    0 讨论(0)
  • 2021-02-14 12:43

    I guess it's related to default view name resolution.

    If you want a value returned by your handler method to be encoded as response body (in JSON, XML, etc), you need to annoate the method with @ResponseBody, or annotate the whole controller with @RestController (in Spring 4.x).

    Otherwise, Spring tries to render a view with your return as model attribute. And since you didn't provide a name of a view to render, Spring tries to deduce it from request URL.

    0 讨论(0)
  • 2021-02-14 12:46

    I had the same problem.

    I fixed it by replacing @Controller with @RestController.

    0 讨论(0)
  • 2021-02-14 12:48

    In addition to @axtvat, pay attention to the differences between @Cotroller and @RestController, @RestController fixed the issue for me. https://dzone.com/articles/spring-framework-restcontroller-vs-controller

    0 讨论(0)
提交回复
热议问题