除了使用org.springframework.web.bind.annotation.RestController的GWT WebApplication外,我还想公开一个简单的REST API。因此,我在web.xml中添加了另一个servlet并将其映射到/app/restapi/*

<!-- ... -->
<servlet>
    <servlet-name>xsrf</servlet-name>
    <servlet-class>com.google.gwt.user.server.rpc.XsrfTokenServiceServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>xsrf</servlet-name>
    <url-pattern>/app/xsrf</url-pattern>
</servlet-mapping>

<!-- New servlet for my REST API -->
<servlet>
    <servlet-name>mobile-dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mobile-dispatcher</servlet-name>
    <url-pattern>/app/restapi/*</url-pattern>
</servlet-mapping>


这是应该处理请求的控制器:

@RestController
@RequestMapping("/app/restapi")
public class RestaurantController {

    class ReturnValue {
        public String str = "1337";
    }

    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "/test", method = RequestMethod.GET, produces = { "application/json" })
    public ReturnValue getTest() {
        return new ReturnValue();
    }
}


问题是请求http://localhost:8080/app/restapi/test给我的只是:

16:14:22.497 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'mobile-dispatcher' processing GET request for [/app/restapi/test]
16:14:22.497 [http-bio-8080-exec-8] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /test
16:14:22.497 [http-bio-8080-exec-8] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/test]
16:14:22.497 [http-bio-8080-exec-8] WARN  o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/app/restapi/test] in DispatcherServlet with name 'mobile-dispatcher'
16:14:22.497 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request


我不知道为什么。因此:我在这里做错了什么,我该如何按预期进行这项工作?

最佳答案

您是这样配置servlet映射的

<servlet-mapping> <servlet-name>mobile-dispatcher</servlet-name> <url-pattern>/app/restapi/*</url-pattern></servlet-mapping>

这是您的应用程序的根URL。当使用RequestMapping("/app/restapi")配置RestController时,这意味着控制器将监听http://domain/app/restapi/app/restapi
只需从控制器定义中删除("/app/restapi"),就可以了。

在启动时,Spring MVC框架应该将其侦听的URL映射打印到控制台,您应该在那里看到控制器的映射。

关于java - 为什么找不到处理程序方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31773649/

10-12 05:10