2

I'm trying to implement a REST endpoint between two non-web applications with all configuration in XML files.
I've created a simple controller with a method that only returns "OK", so I can run some tests using Postman.
Unfortunately, the endpoint is not being created.

I did some research and I found out that I need to add the "context" tag with the component-scan pointing to the controller package for it to work.
But my current implementation is not enough for it to work:

<context:component-scan base-package="com.app.REST"/>

My controller class is:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

    @RequestMapping("/test")
    @ResponseBody
    public String test(){
        return "OK";
    }

}

My question is: is there any way to create a REST endpoint without annotating the main class with @SpringBootApplication? If yes, what am I missing? Is it something in my XML file or somewhere else?

MiguelKVidal
  • 1,498
  • 1
  • 15
  • 23
JomJ
  • 23
  • 5
  • 1
    How is the spring boot application able to boot up without a main class? – Thomas Verhoeven Sep 01 '20 at 14:51
  • Hi & welcome to [SO]! "My question is, is there anyway to create an endpoint REST without the main class annotated with @SpringBootApplication?" - [null problemo!](https://stackoverflow.com/q/46877581/592355) But to run those "spring applications" we formerly(before spring boot) used things called "web application servers" (then packaged the app into a ".war" and deployed it there...where a (already running main method, hopefully) brought up some endpoint.) – xerx593 Sep 01 '20 at 15:06
  • "if yes, is there any other information that I'm missing in xml configuration file?" - I am missing some (xml) parent element, "headers" ...but the content could be sufficient. (package of controller is `com.app.REST` ?) – xerx593 Sep 01 '20 at 15:07

1 Answers1

1

For enabling the MVC functionality you need to instruct spring to scan for your controllers this is done via the <mvc:annotation-driven /> tag.

Also as the DispatcherServlet is taking care of your requests you need to add the correct configuration for it in your web.xml

....
<servlet>
        <servlet-name>my-dispatcher-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:web-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher-servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

LE:

<mvc:annotation-driven> is similar to <context:component-scan> they register all beans annotated with @Controller, @Component, @Service, @Repository and @Bean...

The main difference with mvc:annotation-driven is that it also creates some extra beans which take care of the endpoint registration in the dispatcherServlet (the HandlerMapping, HandlerAdapters and some default conversionServices needed to represent your data received in your controllers for example)

Alex Ciocan
  • 2,272
  • 14
  • 20
  • 1
    Alex Ciocan, Thank you very much for your help. After adding those info to the web.xml and creating a new dispacher xml file, it finally worked! – JomJ Sep 01 '20 at 18:39
  • Hi Jomj, don't forget to accept the answer as correct. Thanks for the upvote. Happy coding :) – Alex Ciocan Sep 02 '20 at 20:59