2016-01-13 62 views
1

我正在尝试处理Spring调度程序servlet中的请求。获取未找到具有URI的HTTP请求的映射

我的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"; 
    } 
} 
+0

我认为这个问题会在你提到servletMapping而不是实体时解决。 – Jango

+0

它可能是未声明的HTTP方法?我在日志中看到“methods = []”。 – Berger

+0

@Berger在这种情况下弹簧会告诉方法不支持 –

回答

1

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

localhost:8080/TestProject/entities/entities/add 

这是因为在URL中的第一个“实体”是越来越因/实体消费/ *在web.xml模式。在使用这个字符串之后,剩余的路径uri会发送到调度程序。在这种情况下,实体/添加到调度程序,它工作正常。


localhost:8080/TestProject/entities/add 

而由你“实体”中提到的URL将被占用,只有“加”是留给其调度没有映射。

如果你有一个servlet映射类似以下内容:

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

则一般与此模式的所有春天的请求映射,URL将是这样的:

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

的URL /实体/添加请求映射,这将是:

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



相关的类,方法名称和代码片段显示消耗从Spring源代码发生的地方。

我找不到链接。所以我直接进入代码。如果你为了遍历这些提到的类和方法,你可以看到,为什么它是由URI路径消耗:
1. org.springframework.web.servlet.DispatcherServlet getHandler:

从调度Servlet和相关类

片段(HttpServletRequest的请求)
2. org.springframework.web.servlet.handler.AbstractHandlerMapping getHandler(HttpServletRequest的请求)
3. org.springframework.web.servlet.handler.AbstractHandlerMapping getHandlerExecutionChain(对象的处理程序,的HttpServletRequest请求) String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
4. org.springframework.web.util.UrlPathHelper getLookupPathForRequest(HttpServletRequest request)。 this.alwaysUseFullPath = false的默认值为false。从路径uri消费发生。你可以在变量“休息”这将包含我们的春天请求映射/实体/添加在这篇文章。
5. org.springframework.web.util.UrlPathHelper getPathWithinServletMapping(HttpServletRequest请求) 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消费的。

+0

你确定它被消耗?并且'* .do'没有被消耗? – Betlista

+0

最后一个条目不会像* .do –

+1

那样被占用,请查看此链接的最后一篇文章http://www.coderanch.com/t/640094/Spring/Multiple-Dispatcher-Servlet。试图找到一个更好的链接 –

0

我可以看到现在的区别...

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

VS

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

您不必RequestMappingEntitiesController ...你知道大概,value@Controller是组件的逻辑名称...

注:基于prem kumar's answer我不知道这是否是一个错误或设计...

相关问题