Issue
I have Spring MVC project to develop sample REST api. It was running when I deploy app in tomcat. Now, I wanted to update the project with Spring boot. I have created the main spring boot application class which is as below at package: com.abc.demo.config
@EnableAutoConfiguration // Sprint Boot Auto Configuration
@ComponentScan(basePackages = "com.abc.demo")
@EnableWebMvc
public class Application extends SpringBootServletInitializer {
private static final Class<Application> applicationClass = Application.class;
private static final Logger log = LoggerFactory.getLogger(applicationClass);
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
}
My controller is at com.abc.demo.Controller which is like below:
@RestController
public class MyController {
static final Logger logger = Logger.getLogger(MyController.class);
@Inject
private MyService myService;
@RequestMapping(value = "/mytask/{task}", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public int myTask(@PathVariable String task) {
return myService.service(task);
}
}
When I deploy the application in local tomcat without adding spring boot, it was deploying and I am able to see the response from the service. (I had web.xml and dispatcher servlet as well while app was running). But, when I added spring boot and run the app from command line, app is running (I have run some threads when app starts, when I can see running). But when I do curl, request is not coming to controller at all and seeing the URI not found log in console as :No mapping found for HTTP request with URI as [/[myservice-name]/myTask Did I miss something here? Appreciate if someone points out the things I missed.
Solution
Work with the framework not around it. Move your Application
to com.abc.demo
(or the actual root package of your project). then remove most of it. All your annotation can be replaced by a simple @SpringBootApplication
. Don't create a DispatcherServlet
Spring Boot already does that for you as well as configuration Spring MVC.
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
private static final Class<Application> applicationClass = Application.class;
private static final Logger log = LoggerFactory.getLogger(applicationClass);
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
}
You can also polish your controller a little as you have @RestController
you don't need the @ResponseBody
.
@RestController
public class MyController {
static final Logger logger = Logger.getLogger(MyController.class);
@Inject
private MyService myService;
@RequestMapping(value = "/mytask/{task}", method = RequestMethod.GET, produces = "application/json")
public int myTask(@PathVariable String task) {
return myService.service(task);
}
}
Finally I would say your url you are using is wrong. By default, when running from the command line, the application is available as the root application i.e. on /
and not on /myservice/mytask/{task}
.
So try curl http://localhost:8080/mytask/123
instead of including the application name.
If you do want /myservice
to be part of the URL you can specify a server.context-path
in the application.properties
for your application.
server.context-path=/myservice
Then it will be prepended to the URL.
Answered By - M. Deinum
Answer Checked By - Willingham (JavaFixing Volunteer)