-1

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?

Andrea Grimandi
  • 631
  • 2
  • 8
  • 32

1 Answers1

2

You made a little error on your controller definition. You wrote:

@Controller("/locale")
public class LocaleController {

    @Autowired
    private LocaleService localeService;

    @RequestMapping(value = "/labels", method = RequestMethod.GET)
    public @ResponseBody List<Label> getAll() {
        return localeService.getAll();
    }
}

This means that the bean who spring will create will have the name "/locale"; if you want that the controller "answers" to the path /locale/labels you must write the Controller in this way:

@Controller
@RequestMapping("/locale")
public class LocaleController {

    @Autowired
    private LocaleService localeService;

    @RequestMapping(value = "/labels", method = RequestMethod.GET)
    public @ResponseBody List<Label> getAll() {
        return localeService.getAll();
    }
}

In this way you are telling spring that the controller LocaleController will answers to all the requests with prefix /locale; each method inside it will be called according to the calling path

Angelo Immediata
  • 6,635
  • 4
  • 33
  • 65