0

I am maintaining a Spring 4.3 MVC app, upgrading it is a complicated and long term task. For my setup, XML-based, I did not find much on the internet, mostly Spring Boot stuff. I tried adding a @RestController in the current MVC app, but it did not work.

With @RestController in Spring 4.3, can it be used simultaneously with a Spring web-mvc controller in the same servlet (i.e. web.xml and business-servlet.xml), or must it be defined in a separate servlet which contains @RestControllers only?

5
  • 1
    Yes there is nothing preventing you from "mixing" those. However the annotation processing, on older versions of Spring, is opt-in. So for the annotation style to work you will need to enable it, adding an <mvc:annotation-driven /> to the business-servlet.xml (and ofcourse adding the namespace to the XML file). Commented Nov 24 at 9:45
  • Thanks for your help @M.Deinum, it helped me not give up on the idea. I was apparently confusing several apps a bit, so the app in question was missing a DispatcherServlet, but once that was in place. It worked like a charm. Commented Nov 26 at 7:46
  • A big thanks to @MarcoBehler article link it helped me remember about the DispatcherServlet and how spring works under the hood. Commented Nov 26 at 7:58
  • If there was no DispatcherServlet to start with how was this a Spring MVC application to start with?! Commented Nov 26 at 7:59
  • @M.Deinum, long answer short I mixed things up, sorry. The actual question was still valid and real, "is it possible...", cause I didnt really know if or how to do it in Spring prior to Spring Boot. In the actual stack, I mixed things: 5 apps are Spring MVC, but v3.0.2. The last is Spring 4.3, but Spring with Apache CXF. Since I now know how it works, I can create the REST API I need and upgrade the MVC apps from Spring 3.x to 4.3 and I port an Ajax API to the RestController annotation type as well. Commented Nov 26 at 8:47

1 Answer 1

0

Yes, in Spring Framework 4.3 with XML config, you can use @RestController and traditional @Controller together in the same servlet. Just make sure <mvc:annotation-driven /> and component scanning are enabled. No need for a separate servlet.

New contributor
Yassine Hamdouni is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.