In my web.xml, I rendered the servlet in / api as follows:
<servlet-mapping>
<servlet-name>todo</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>todo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
So, for the controller using it @RequestMapping(value = "/todo", method = RequestMethod.GET), it actually maps to "/ api / todo"
But in the test, when I call mockMvc.perform(get("/api/todo/")), I got 404. I need to call get("/todo/")to get the correct match in the test. I hope to display the controller in the test, as in a real scenario. Is it possible?
I use WebMvcConfigurerAdapterin the test and I tried
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/api/**").addResourceLocations("/");
}
but it didn’t work.
source
share