获取没有使用URI的HTTP请求的映射

时间:2016-01-13 10:58:15

标签: 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(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";
    }
}

2 个答案:

答案 0 :(得分:1)

我现在可以看到差异......

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

VS

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

RequestMapping中没有EntitiesController ...正如您所知,value中的@Controller是该组件的逻辑名称......

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

答案 1 :(得分:1)

如果你尝试点击以下网址,它就会有用。

localhost:8080/TestProject/entities/entities/add

这是因为第一个&#34;实体&#34;因为web.xml中的/ entities / * pattern而在url中被占用。使用此字符串后,剩余路径uri将转至调度程序。在这种情况下,实体/添加到调度程序,它工作正常。


localhost:8080/TestProject/entities/add 

而您提及的网址&#34;实体&#34;被消费,只有&#34;添加&#34;留给调度员没有映射。

如果您有如下的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请求)  String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
4. org.springframework.web.util.UrlPathHelper getLookupPathForRequest(HttpServletRequest request)。 this.alwaysUseFullPath = false的默认值为false。从路径uri消耗发生。您可以使用变量&#34; rest&#34; ,它将在此帖子中包含我们的弹簧请求映射,例如/ 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中找到它的消耗方式。