如何在Eclipse中创建简单Restful Web服务并解决web.xml报错问题
解决方案
报错根因
两个报错均由web-app标签的版本声明与引用的schema文件不匹配导致:
你声明了version="2.5",但xsi:schemaLocation引入的是Servlet 2.4版本的schema文件,2.4版本的规则不允许2.5的版本号,同时对允许的标签、标签顺序的约束和2.5版本不同,因此触发了display-name不合法的报错。
两种修复方案
根据你项目依赖的Servlet版本选择对应方案即可:
方案1:使用Servlet 2.5版本(推荐,保留display-name标签)
将web-app开头的声明替换为符合2.5规范的正确配置即可,完整修正后的web.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>JAX-RS-Starter</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
方案2:保持使用Servlet 2.4版本
将version属性改为2.4,同时删除不被2.4规范支持的display-name标签即可,完整修正后的web.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
修正后在Eclipse中右键项目选择Maven -> Update Project(如果是Maven项目)或者刷新项目,报错就会消失。
内容的提问来源于stack exchange,提问作者Mohibur_Cou
相关产品推荐
相关产品推荐

