java - 找不到带有 URI 的 HTTP 请求的映射

标签 java spring servlets

我正在尝试在 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 看起来:

@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";
    }
}

最佳答案

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

localhost:8080/TestProject/entities/entities/add

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


localhost:8080/TestProject/entities/add 

而对于您提到的网址,“实体”会被消耗,并且只有“添加”,而调度程序没有映射。

如果您有如下所示的 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



相关类、方法名称和代码片段,以显示 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(对象处理程序,HttpServletRequest请求) 字符串lookupPath = 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 中消耗。

关于java - 找不到带有 URI 的 HTTP 请求的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34764858/

相关文章:

Java错误缺少返回语句

java - Gradle:如何从 WAR 中排除 JAR?

java - 不可解析的父 POM

java - 如何使用Spring的i18n机制?

java - Spring boot 在 servlet 上下文之外获取应用程序基础 url

java - 有人能告诉我为什么这个 if 语句总是给我 false 吗?

java - 方法在 netbeans 分析器中花费 0ms

java - 在 Spring 应用程序中使用 CXF 自动发现 JAX-RS 资源

java - AWS EC2 实例 : Launch Multiple Instance

jsp getServletContext() 错误