I am rewriting an API backend from Nodejs to Spring. This is my first Spring REST API backend application ever. I want to make it as simple as possible in order to avoid using .xml(s)
by following the Spring documentation
This backend, since now, has only one controller and one service. The entire application it's not meant to serve any web pages, but REST API only.
I started up by writing a simple configuration based on the AbstractAnnotationConfigDispatcherServletInitializer
and the WebMvcConfigurerAdapter
:
@Configuration
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{MvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
//---------------------------------------------------------------
@Configuration
@EnableWebMvc
@ComponentScan("com.root.package")
@EnableAspectJAutoProxy
public class MvcConfig extends WebMvcConfigurerAdapter {
//I think I may not need anything inside here, right?
}
... and the controller as well...
@Controller("/locale")
public class LocaleController {
@Autowired
private LocaleService localeService;
@RequestMapping(value = "/labels", method = RequestMethod.GET)
public @ResponseBody List<Label> getAll() {
return localeService.getAll();
}
}
It starts on Tomcat, I could debug it. Log shows Mapped "{[/labels],methods=[GET]}"
and Mapped URL path [/locale] onto handler '/locale'
but once I call that by postman, I see No mapping found for HTTP request with URI [/myapp/locale/labels] in DispatcherServlet with name 'dispatcher'
.
Every research mainly headed me to Why does Spring MVC respond with a 404 and report "No mapping found for HTTP request with URI [...] in DispatcherServlet"? -but- is a bit verbose and... almost every concept written is based on the Spring documentation that I was already following since I do not deep know Spring :)
I am sure that my issue has an easier solution. What I am missing?