Spring-mvc servlet映射,servlet 1可能有“/”,servlet 2有“/ server /”吗?

时间:2013-03-19 09:12:40

标签: servlets spring-mvc mapping url-pattern

目前我有以下web.xml配置:

<servlet>
    <servlet-name>Website</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Website</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>Server</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Server</servlet-name>
    <url-pattern>/server/</url-pattern>
</servlet-mapping>

至于两个示例控制器:

@Controller
public class ApiRequests {
    protected final Logger logger = Logger.getLogger(ApiRequests.class);

    @RequestMapping(value = "api.json", produces = {"application/json"}, method = RequestMethod.GET)
    @ResponseBody
    public String handleActionJson(final HttpServletRequest request){
        return "{test: 'blaat'}";
    }
}

@Controller
@RequestMapping("/")
public class IndexController {
    @RequestMapping("*.html")
    public void showIndex(){

    }
}

现在我的问题是,当我尝试调用/server/api.json url时,服务器不会给出json响应,而是它给了我以下错误:

PageNotFound:1108 - No mapping found for HTTP request with URI [/WorldDefense/server/api.json] in DispatcherServlet with name 'Website'

这基本上意味着它试图在网站servlet中搜索/server/api.json而不是服务器serlvet,至于我的问题:是否可以让这个设置工作? (所以/ server /映射到服务器servlet和网站servlet的所有其他url组合)

修改1 我更新了代码以反映Costi Ciudatu建议的更改,但它仍然不起作用。我删除了@RequestMapping(“/ server”),现在只在handleActionJson方法中有@RequestMapping。两者都导致了这些错误:

10:57:26,046  WARN PageNotFound:1108 - No mapping found for HTTP request with URI [/WorldDefense/server/server/api.json] in DispatcherServlet with name 'Website'
10:57:40,509  WARN PageNotFound:1108 - No mapping found for HTTP request with URI [/WorldDefense/server/api.json] in DispatcherServlet with name 'Website'

根据tomcat日志进行映射:

服务器的servlet

11:03:49,655  INFO RequestMappingHandlerMapping:178 - Mapped "{[/api.json],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}" onto public java.lang.String com.world2.worlddefense.server.controllers.ApiRequests.handleActionJson(javax.servlet.http.HttpServletRequest)
11:03:50,125  INFO RequestMappingHandlerMapping:178 - Mapped "{[/api.json],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}" onto public java.lang.String com.world2.worlddefense.server.controllers.ApiRequests.handleActionJson(javax.servlet.http.HttpServletRequest)

网站-的servlet

11:03:50,380  INFO RequestMappingHandlerMapping:178 - Mapped "{[//*.html],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void com.world2.worlddefense.website.controllers.IndexController.showIndex()
11:03:50,381  INFO RequestMappingHandlerMapping:178 - Mapped "{[/login],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.world2.worlddefense.website.controllers.TempTestController.showTest()

1 个答案:

答案 0 :(得分:1)

您可以在同一DispatcherServlet中定义多个web.xml,并根据需要进行映射。但是,servlet映射不应反映在控制器的@RequestMapping中。您的控制器始终映射到相对于servlet映射的路径;在您的情况下,如果您尝试/WorldDefence/server/server/api.json ...

,我认为您可以获得JSON响应

如果您希望将不同的控制器与两个调度程序相关联,您只需要确保在相应的应用程序上下文中加载了正确的控制器:Website-servlet.xmlServer-servlet.xml以防您坚持使用这个惯例。

长话短说,ApiRequests的映射应该是'/',而不是'/ server',并且该控制器只应包含在“服务器”调度程序上下文中。

作为旁注,自3.2.x以来,.json扩展由ContentNegotiationManager处理,因此您的控制器应返回一些域对象,该对象将由Spring HttpMessageConverter机制透明地封送,因此能够毫不费力地生成JSON以外的媒体类型。