【问题标题】:Getting No mapping found for HTTP request with URI获取没有找到带有 URI 的 HTTP 请求的映射
【发布时间】:2016-04-18 07:39:44
【问题描述】:

我正在尝试在 Spring 调度程序 servlet 中处理请求。

我的 web.xml 有 servlet:

<servlet>
    <servlet-name>ApplicationDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/SpringConfig/WebApplication.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>ApplicationDispatcher</servlet-name>
    <url-pattern>*.json</url-pattern>
    <url-pattern>*.do</url-pattern>
    <url-pattern>/entities/*</url-pattern>
</servlet-mapping>

我的控制器看起来:

@Controller(value="entities")
public class EntitiesController {
    private static final Logger LOGGER = Logger.getLogger(EntitiesController.class);

    @Autowired
    private IEntityDataService iEntityDataService;

    @RequestMapping("/list")
    public String displayAllEntities() {
        LOGGER.info("Displaying entity dashboard");
        return "entity_landing";
    }

    @RequestMapping("/display")
    public String displayCheckpointDashboard(Integer id) {
        LOGGER.info("Displaying checkpoint dashboard for id " + id);
        return "entity";
    }

    @RequestMapping("/update")
    public String displayUpdateEntity(Integer id) {
        System.out.println("Update id " + id);
        return "new_entity";
    }

    @RequestMapping("/add")
    public String displayNewEntity() {
        LOGGER.info("Displaying new entity page");
        return "new_entity";
    }
}

我在我的应用程序日志中看到以下日志:

2016-01-13 16:15:12 INFO  RequestMappingHandlerMapping:180 - Mapped "{[/entity/add/entityDetails.do],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.test.EntityController.saveEntityDetails(com.test.vo.EntityCheckpointsVo)
2016-01-13 16:15:12 INFO  RequestMappingHandlerMapping:180 - Mapped "{[/entities/list],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.test.EntitiesController.displayAllEntities()
2016-01-13 16:15:12 INFO  RequestMappingHandlerMapping:180 - Mapped "{[/entities/add],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.test.EntitiesController.displayNewEntity()
2016-01-13 16:15:12 INFO  DispatcherServlet:476 - FrameworkServlet 'ApplicationDispatcher': initialization completed in 950 ms
2016-01-13 16:15:12 WARN  PageNotFound:1116 - No mapping found for HTTP request with URI [/TestProject/entities/add] in DispatcherServlet with name 'ApplicationDispatcher'
2016-01-13 16:15:17 WARN  PageNotFound:1116 - No mapping found for HTTP request with URI [/TestProject/entities/add] in DispatcherServlet with name 'ApplicationDispatcher'
2016-01-13 16:17:04 WARN  PageNotFound:1116 - No mapping found for HTTP request with URI [/TestProject/entities/add] in DispatcherServlet with name 'ApplicationDispatcher'

我没有任何线索,因为日志显示/entities/add 已注册。我可以访问其他 URL,例如 localhost:8080/TestProject/entity/add/entityDetails.do,但我无法访问 localhost:8080/TestProject/entities/add。

请帮帮我。

谢谢

更新:

来自 EntityController 的片段

@Controller
@RequestMapping(value = "/entity")
public class EntityController {
    @RequestMapping(value = "/add/entityDetails.do", method = RequestMethod.GET)
    public String saveEntityDetails(EntityCheckpointsVo entityCheckpointsVo) {
        return "success";
    }
}

【问题讨论】:

  • 我认为一旦你在 servletMapping 中提到 / 而不是实体,问题就会得到解决。
  • 会不会是未声明的 HTTP 方法?我在您的日志中看到“methods=[]”。
  • @Berger 在这种情况下 spring 会告诉方法不支持
  • 我注意到您没有使用基本/ 来进行调度程序servlet 本身的映射,并且您有多种种类 的映射模式。为什么,是否可以只使用 Spring Boot 并完全跳过 web.xml 的复杂性?
  • @Jango:当我提到 / 作为 url 映射时,/TestProject/index.html 没有被映射。

标签: java spring servlets


【解决方案1】:

如果您尝试点击以下网址,它将起作用。

localhost:8080/TestProject/entities/entities/add

这是因为 url 中的第一个“实体”由于 web.xml 中的 /entities/* 模式而被消耗。使用此字符串后,剩余的路径 uri 将转到调度程序。在这种情况下,entities/add 转到调度程序,它工作正常。


localhost:8080/TestProject/entities/add 

而对于您提到的 url,“实体”被消耗,只剩下“添加”,调度程序没有映射。

如果您有如下的 servlet 映射:

<url-pattern>/abc/def/*</url-pattern>

那么一般来说,对于任何使用这种模式的 spring 请求映射,url 都会像:

localhost:8080/TestProject/abc/def/{custom request mapping}

对于 url /entities/add 请求映射,它将是:

localhost:8080/TestProject/abc/def/entities/add



相关的类、方法名和代码sn-ps,从spring源代码中显示消费发生的地方。

我找不到链接。因此,我直接进入代码。如果您按顺序遍历这些提到的类和方法,您可以了解它为什么从路径 uri 中消耗:

Dispatcher Servlet 和相关类的片段:
1. org.springframework.web.servlet.DispatcherServlet getHandler(HttpServletRequest request)
2. org.springframework.web.servlet.handler.AbstractHandlerMapping getHandler(HttpServletRequest request)
3. org.springframework.web.servlet.handler.AbstractHandlerMapping getHandlerExecutionChain(Object handler, HttpServletRequest request) 字符串查找路径 = this.urlPathHelper.getLookupPathForRequest(request);
4. org.springframework.web.util.UrlPathHelper getLookupPathForRequest(HttpServletRequest request) 。 this.alwaysUseFullPath=false 的默认值为 false。从路径 uri 发生消费。您可以使用变量 "rest" 来包含我们的 spring 请求映射,例如本文中的 /entities/add。
5. org.springframework.web.util.UrlPathHelper getPathWithinServletMapping(HttpServletRequest request) String path = getRemainingPath(pathWithinApp, servletPath, false);

public String getLookupPathForRequest(HttpServletRequest request) {
    // Always use full path within current servlet context?
    if (this.alwaysUseFullPath) {  // default this.alwaysUseFullPath=false
        return getPathWithinApplication(request);
    }
    // Else, use path within current servlet mapping if applicable
    String rest = getPathWithinServletMapping(request);
    if (!"".equals(rest)) {
        return rest;
    }
    else {
        return getPathWithinApplication(request);
    }
}

从这里您可以轻松地更深入地了解它是如何从路径 uri 消耗的。

【讨论】:

  • 你确定它被消耗了吗?而且*.do没有被消费?
  • 最后一个条目不会像 *.do 那样被消耗
  • 查看此链接coderanch.com/t/640094/Spring/Multiple-Dispatcher-Servlet 中的最后一篇文章。试图找到更好的链接
  • 我自己测试了一下,你说的没错,已经消耗掉了。
  • 我已经编辑了我的答案。它现在具有类、方法和一些 sn-ps 的名称,以显示在代码中发生这种消耗的位置。有兴趣的可以看看
【解决方案2】:

我现在可以看出区别了...

@Controller(value="entities")
public class EntitiesController {

@Controller
@RequestMapping(value = "/entity")
public class EntityController {

您在EntitiesController 中没有RequestMapping...您可能知道,@Controller 中的value 是组件的逻辑名称...

注意:基于prem kumar's answer我不确定这是错误还是设计......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 2016-02-05
    相关资源
    最近更新 更多