1

I've been trying to setup a Spring MVC controller but when I try to make a GET request, I get a 404 error.

I created a working test example here: https://github.com/Jardo-51/zk-spring-mvc-test

When I run the application on Tomcat and try to make a GET request to: http://localhost:8080/zk-spring-mvc-test/api/v0/foo, I get a 404 error and the logs say:

WARNING: No mapping found for HTTP request with URI [/zk-spring-mvc-test/api/v0/foo] in DispatcherServlet with name 'dispatcher-api'`

I've been trying to fix it according to this answer, and found out that the controller is mapped correctly because the logs on startup say:

INFO: Mapped "{[/zk-spring-mvc-test/api/v0/foo],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.lang.String> com.jardoapps.zkspringmvctest.controllers.FooController.method()

The app uses ZK framework which needs its own servlets so maybe there is a conflict with the DispatcherServlet. Please see my example app for more details (it contains only the necessary code).

Here is the web.xlm (Spring context and MVC config are at the top).

Here is the controller class.

Jardo
  • 1,939
  • 2
  • 25
  • 45

1 Answers1

1

Simply replace @RequestMapping("zk-spring-mvc-test/api/v0/foo") with @RequestMapping("/v0/foo") in your FooController class.

The reason is that the path that you specify into the @RequestMapping annotation is the part of the request's URL beyond the part that called the servlet.

You defined DispatcherServlet's mapping as:

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

So we have zk-spring-mvc-test that is the context root (this is deploy dependent), /api/ that calls the Spring DispatcherServlet, and finally /v0/foo that should be mapped by your controller:

@RestController
@RequestMapping("/v0/foo")
public class FooController {

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<String> method() {
        return ResponseEntity.ok().body("OK");
    }
}

You can see Spring MVC configure url-pattern for further information.

Robert Hume
  • 1,129
  • 2
  • 14
  • 25